1001 A+B Format

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
#include 
#include 
int main()
{
	int a,b;
	scanf("%d %d",&a,&b);
	int sum=a+b;
	char arr[12];
	int i=0;
	int cnt=0;
	
	if(sum>0)
	{
	while(sum){
		arr[i++]=sum%10+'0';
		sum/=10;
		cnt++;
		if(cnt%3==0 && sum)
			arr[i++]=','; 
	}
	while(i>0){
		printf("%c",arr[--i]);
	}
	} else if(sum<0){
	sum=abs(sum);
	while(sum){
		arr[i++]=sum%10+'0';
		sum/=10;
		cnt++;
		if(cnt%3==0 && sum)
			arr[i++]=','; 
	}
	printf("-");
	while(i>0){
		printf("%c",arr[--i]);
	}
	} else
		printf("%d",sum);		
	return 0;
}

 用自己的现有知识琢磨出来的,一定还有其它方法。

你可能感兴趣的:(PAT,(Advanced,Level),Practice,算法,数据结构)