1001 A+B Format (20 分)

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 106a,b106. 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

Submit:

#include <stdio.h>
#include <string.h>

int main() {
    
    int a,b,sum;
    char str[20];
    scanf("%d %d",&a,&b);
    sum = a + b;
    sprintf(str,"%d",sum);//字符串地址,第三个参数类型,写入的数据;该函数为清空写入
    int len = strlen(str);
    for (int i=0;i<len;i++) {
        printf("%c",str[i]);
        if (str[i] == '-') continue;//第一个字符是负号,则执行下一次循环
        if ((i+1)%3 == len%3 && i != len-1) printf(",");//12345678 8位 需要输出为 12,345,678 即 i=1时,2%3==2,8%3==2需要输出‘,’
    }
    return 0;
}

参考:

柳婼-https://blog.csdn.net/liuchuo/article/details/54561626

昵称五个字-https://blog.csdn.net/a617976080/article/details/89676670

原文地址:https://www.cnblogs.com/cgy-home/p/15097677.html