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

相关推荐

不愿透露姓名的神秘牛友
07-04 14:35
点赞 评论 收藏
分享
牛客92804383...:在他心里你已经是他的员工了
点赞 评论 收藏
分享
zYvv:双一流加大加粗再标红,然后广投。主要是获奖荣誉不够,建议开始不用追求大厂,去别的厂子刷下实习。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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