思路:
数的范围没有超long long的范畴,故不需要用字符串处理;
将每一位取出保存在vector或array中,当剩余位数为3的整数倍的时候输出逗号;
第四个测试点为0+0,注意考虑边界值。
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 −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
//1001
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
int main(){
ll a,b;
cin>>a>>b;
ll c =a+b;
if(c<0){
cout<<"-";
}
if(c==0){
cout<<0;
}
c=abs(c);
vector<int> s;
int d;
while(c>0){
d=c%10;
s.push_back(d);
c=c/10;
}
int k=s.size();
for(int i=k-1;i>=0;i--){
if(i%3==2&&i!=k-1){
cout<<",";
}
cout<<s[i];
}
cout<<endl;
return 0;
}