码迷,mamicode.com
首页 > 其他好文 > 详细

1001. A+B Format (20) (数学啊 ZJU_PAT)

时间:2014-11-27 22:07:43      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:pat   zju   数学   

题目链接:http://www.patest.cn/contests/pat-a-practise/1001


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 <cstdio>
#include <cstring>
int main()
{
    int a, b, c;
    int d[1017];
    while(~scanf("%d%d",&a,&b))
    {
        int flag = 0;
        c = a+b;
        int l = 0;
        if(c == 0)
        {
            printf("0\n");
            continue;
        }
        if(c < 0)
        {
            flag = 1;
            c = -c;
        }
        for(int i = 0; ; i++)
        {
            int tt = c%10;
            c/=10;
            //printf("c::%d\n",c);
            d[l++] = tt;
            //printf("tt::%d\n",tt);
            if(c == 0)
                break;
        }
        int k = 0;
        if(flag)
            printf("-");
        int cont = l;
        for(int i = l-1; i >= 0; i--)
        {
            printf("%d",d[i]);
            cont--;
            if(cont%3==0 && cont!=0 && i!=0)
                printf(",");
        }
        printf("\n");
    }
    return 0;
}
/*
-1000000 9
-1000000 -1000000
*/


1001. A+B Format (20) (数学啊 ZJU_PAT)

标签:pat   zju   数学   

原文地址:http://blog.csdn.net/u012860063/article/details/41554115

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!