美文网首页
【PAT-甲级-C++】1001. A+B Format (20

【PAT-甲级-C++】1001. A+B Format (20

作者: linghugoogle | 来源:发表于2018-01-22 21:20 被阅读38次

    1001. A+B Format (20)

    时间限制
    400 ms
    内存限制
    65536 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
    

    2、注意

    数组的编号还是一脸懵逼

    3、代码

    #include<iostream>
    using namespace std;
    int main() {
        long long a, b,c;
        cin >> a >> b;
        int i=1,j,k=1;
        char ch[20] = {'0'};
        c = a + b;
        if (c == 0) {
            cout << 0;
            return 0;
        }
        if (c < 0) {
            cout << "-";
            c = -c;
        }
        while (c > 0) {
            j = c % 10;
            c = c / 10;
            ch[k] = j +'0';
            if (i % 3 == 0)
                ch[++k] = ',';
            ++i;
            ++k;
        }
        for (j = k-1;j >= 1;--j) {
            if (j == k-1&&ch[j] == ',')
                continue;
            cout << ch[j];
        }
        system("pause");
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:【PAT-甲级-C++】1001. A+B Format (20

          本文链接:https://www.haomeiwen.com/subject/svvraxtx.html