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
知识点:主要运用字符串处理。to_string将数字转换为字符串。另外为了避免当数字的位数恰好为三的倍数,i=0时输出‘,’,所以在判断条件上加了i!=0;
#include <iostream>
#include <string>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
if(a+b<0)
cout<<'-';
string s=to_string(abs(a+b));
int d=(s.length())%3;
for(int i=0;i<s.length();i++){
if(i!=0&&i%3==d)
cout<<',';
cout<<s[i];
}
}