【PTA 1001】A+B Format
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
开个坑 ,把PTA甲级做一遍。
题意:
计算A+B,输出时每三位数用逗号隔开;
代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define closeio std::ios::sync_with_stdio(false)
#define maxn 100005
int main()
{
int x,y,sum,i,flag=0,cnt=0,t=0;
string s;
cin>>x>>y;
sum=x+y;
if(sum<0)
{
flag=1;
sum*=-1;
}
if(sum==0)
{
cout<<"0"<<endl;
return 0;
}
while(sum)
{
s[cnt++]=sum%10+'0';
sum/=10;
t++;
if(t>=3&&sum)
{
s[cnt++]=',';
t=0;
}
}
if(flag)
cout<<"-";
for(i=cnt-1;i>=0;i--)
cout<<s[i];
cout<<endl;
return 0;
}