首页 > 学院 > 开发设计 > 正文

PAT A1001. A+B Format (20)

2019-11-11 02:45:04
字体:
来源:转载
供稿:网友

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 9Sample Output
-999,991
#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>#define Max 100001using namespace std;int main(){	int a,b,m[100]={0},sum,f=0,k=0,p=0,q=0;	char num[100]={};	int R[100];	for(int i=0;i<100;i++)	{		R[i]=0;	}	scanf("%d%d",&a,&b);	sum=a+b;	if(sum<0) 	{		PRintf("-");		sum=-sum;	}	if(sum==0) printf("0/n");	else {	k=0;	while (sum>0)	{        k++;		m[f++]=sum%10;		sum/=10;		if(sum==0) break;		if(k==3)		{			k=0;			m[f++]=11;		}	}		for(int i=f-1;i>=0;i--)	{		if(m[i]>=10)			printf(",");		else printf("%d",m[i]);	}	printf("/n");}	system("pause");	return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表