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 A+B Format (20 分) 全网最细 题目详解 翻译 完整代码 PAT甲级真题解析_a+b format 测试用例2__Pooooooocky的博客-CSDN博客
//先自己读题目,读不懂的情况下再去百度翻译。本题的意思就是输入两个整数,然后将他们的和按格式输出(从后往前,每三个数字 打一个逗号)。上面一个博客也是一种方法,仅供参考。
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,k=0,j=0;string s,x;
cin>>a>>b;
s=to_string(a+b);
for(int i=s.size()-1;i>=0;i--){
if(s[i]>='0'&&s[i]<='9')
{
j++;
if(j==4)
{ x[k++]=',';x[k++]=s[i];j=1;}
else
x[k++]=s[i];
}
else x[k++]=s[i];
}
for(int i=k-1;i>=0;i--)
cout<<x[i];
return 0;
}