0
点赞
收藏
分享

微信扫一扫

1001 A+B Format

双井暮色 2022-01-08 阅读 66
c++c语言

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 Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.

Output Specification:

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

编译软件:visual studio

编译语言:c语言

参考代码:

#include<cstdio>
int num[10];

int main()
{
    int a, b, sum;
    scanf_s("%d%d", &a, &b);

    sum = a + b;

    if (sum < 0)
    {
        printf("-");
        sum = -sum;
    }

    int len = 0;

    if (sum == 0) num[len++] = 0;

    while (sum)
    {
        num[len++] = sum % 10;
        sum /= 10;
    }

    for (int i = len - 1; i >= 0; i--)
    {
        printf("%d", num[i]);
        if (i > 0 &&i % 3 == 0) printf(",");
    }

    return 0;
}

举报

相关推荐

0 条评论