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.
兩種思路孙技,一種是轉(zhuǎn)字符串從左往右加逗號闸衫,從左往右輸出,再有就是利用循環(huán)從右往左加逗號咐熙,然后從左往右輸出
#include <iostream>
#include <stdio.h>
using namespace std;
void prinf(int a,int b,int i,int z){
if (a==0){
if (z==1)
cout<<"-";
return;
}
b=a%10;
i+=1;
prinf(a/10,b,i,z);
if (i%3==1 && i!=1){
cout<<abs(b)<<",";
}
else{
cout<<abs(b);
}
}
int main(){
int a=0,b=0;
cin>>a>>b;
a=a+b;
if (a>0){
prinf(a,0,0,0);
}
else if (a==0){
cout<<"0";
}
else{
prinf(a,0,0,1);
}
return 0;
}