1001 A+B Format

发布时间:2024年01月06日

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 <stdio.h>
#include <math.h>
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;
}

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

文章来源:https://blog.csdn.net/s1ms1mpleple/article/details/135417243
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。