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

相关推荐

karis_aqa:和hr没关系,都是打工的
点赞 评论 收藏
分享
想玩飞盘的菠萝蜜在春...:上交✌🏻也拒?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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