PAT-A 1001. A+B Format

Calculate a + b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input

-1000000 9

Sample Output

-999,991

程序代码:

#include<stdio.h>
#include<string.h>
void int_char(char c[],int a);
char c[10]={0};
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    a=a+b;
    int_char(c,a);
    if(a<0)
        putchar('-');
    int len = strlen(c);
    int i=len -1;
    for(;i>=0;i--)
        putchar(c[i]);
    return 0;
}
void int_char(char c[],int a)
{
    if(a<0)
        a=-1*a;
    else if(a==0)
        c[0]='0';
    int count = 0;
    int i=0;
    while(a!=0)
    {
        if(count==3)
        {
            c[i]=',';
            i++;
            count = 0;
        }
        c[i]=a%10+'0';
        a=a/10;
        i++;
        count++;
    }
}
全部评论

相关推荐

已老实求offer😫:有点像徐坤(没有冒犯的意思哈)
点赞 评论 收藏
分享
joe2333:怀念以前大家拿华为当保底的日子
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务