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++;
    }
}
全部评论

相关推荐

程序员小白条:找的太晚,别人都是大三实习,然后大四秋招春招的,你大四下了才去实习,晚1年
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务