题目地址

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

作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
    int a,b,c;
    cin>>a>>b;
    c=a+b;
    string s=to_string(c);
    reverse(s.begin(),s.end());
    int t=0;
    for(int i=0;i<s.length();i++){
        if(t==2) {
            s.insert(s.begin()+i+1,',');i++;
        }
        t++;
        if(t==3) t=0;
    }
    reverse(s.begin(),s.end());    
    t=0;
    if(s[0]=='-')t++;
    if(s[t]==',')
    s.erase(t,1);
    cout<<s;
}