【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 −10​6​​≤a,b≤10​6​​. 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;
}

 

全部评论

相关推荐

点赞 评论 收藏
分享
牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务