前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT 甲级 1001 A+B Format

PAT 甲级 1001 A+B Format

作者头像
ShenduCC
发布2018-04-26 17:31:11
6090
发布2018-04-26 17:31:11
举报
文章被收录于专栏:算法修养算法修养算法修养

1001. A+B Format (20)

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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.

Sample Input

-1000000 9

Sample Output

-999,991

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <strstream>

using namespace std;
int a,b;
int main()
{
	while(scanf("%d%d",&a,&b)!=EOF)
	{
		int num=a+b;
		strstream ss;
		string s;
		ss << num;
		ss >> s;
		int len=s.length();
		if(num<0)
			num=(len-1)%3;
		else
			num=len%3;
		for(int i=0;i<num;i++)
		{
			cout<<s[i];
			if(s[i]=='-') num++;
			if(i==num-1&&len>3)
				cout<<",";
		}
		int cnt=0;
		for(int i=num;i<len;i++)
		{
			cout<<s[i];
			if(s[i]<='9'&&s[i]>='0')
				cnt++;
			if(cnt==3)
			{
				if(i!=len-1)
					cout<<",";
				cnt=0;
			}
		}
		cout<<endl;
	}
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-05-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1001. A+B Format (20)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档