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.
两种思路,一种是转字符串从左往右加逗号,从左往右输出,再有就是利用循环从右往左加逗号,然后从左往右输出
#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;
}
网友评论