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
题目大意是计算 a+b 并以标准格式输出总和——也就是说,数字必须用逗号分隔成三组(除非少于四位)
废话不多说,直接上代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
int sum=a+b;
string str=to_string(sum);
int len=str.length();
for(int i=0;i<len;i++)
{
cout<<str[i];
if(str[i]=='-') continue;
else if((i+1)%3==len%3&&i!=len-1) cout<<",";
}
return 0;
}
这是甲级的第一题,学到挺多,
to_string
将一个数强制转换为一个字符串,挺管用的
开始写题解,我相信我甲级一定可以满分,一起加油!!!