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
大概意思应该是两个数相加的和后有逗号的输出吧
程序清单:
#include<stdio.h>
int main()
{
int a,b,fu=0,num[1000];
scanf("%d %d",&a,&b);
int sum=a+b;
if(sum==0)printf("0");
if(sum<0)
{
fu=1;
sum=-sum;
}
int i=0;
while(sum!=0)
{
num[i++]=sum%10;
sum/=10;
}
i--;
if(fu)printf("-");
int count=0;
for(;i>=0;i--)
{
printf("%d",num[i]);
count++;
if(count%3==0&&i!=0)printf(",");
}
}
有挺多错误的,我看网上的代码也不知道自己什么情况没考虑到,请教~