1001 A+B Format (20分)

    科技2026-06-05  11

    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 −10 ​6 ​​ ≤a,b≤10 ​6 ​​ . 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

    这道题主要是让你初始输出,一般比较长的数字我们转化为字符串处理 如果是负数那么“-”照样输出 因为是从后往前数每3位要打一个“,”,那么就找第一个不满3位的且不是最后一位的,在它后面打“,”, 因为i是序号,真正的位数是i+1位,如果字符串为4,那么多出的就是4%3=1,就是一位,那么只要找出前1位,也就是(i+1)%3 == 4%3的那一位就行了

    #include<cstdio> #include<iostream> using namespace std; int main(){ int a, b; scanf("%d%d",&a, &b); string s = to_string(a+b); int len = s.length(); for(int i = 0; i < len; i++){ cout << s[i]; if(s[i] == '-') continue; if((i + 1) % 3 == len % 3 && i != len-1) cout<<","; } return 0; }
    Processed: 0.018, SQL: 9