PAT-1001

1001. A+B Format (20)

 

时间限制

400 ms

内存限制

32000 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

 

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
AC源码:
#include 

using namespace std;

int abs(int x)
{
    return x >= 0 ? x : -x;
}

int main()
{
    int a;
    int b;
    int buf[4];

    while(scanf("%d %d", &a, &b) != EOF)
    {
        int rst = a + b;
        bool mark;
        if(rst >= 0)
        {
            mark = true;
        }
        else
        {
            mark = false;
        }

        int cnt = 0;

        rst = abs(rst);

        if(rst == 0)
        {
            printf("0\n");
            continue;
        }

        while(rst != 0)
        {
            buf[cnt++] = rst % 1000;
            rst /= 1000;
        }

        if(!mark)
        {
            printf("-");
        }

        for(int i = cnt - 1; i >= 0; i--)
        {
            if(i == cnt - 1)
            {
                printf("%d", buf[i]);
            }
            else
            {
                printf(",%03d", buf[i]);
            }
        }

        printf("\n");
    }

    return 0;
}
 
提交结果:

image

 

点评:

刚开始没有考虑结果0,所以只有14分,加了之后15分,通过用例1000 1发现输出格式问题,改正,满分A过。

posted @ 2013-03-26 22:53  crAzyli0n  阅读(224)  评论(0编辑  收藏  举报