1001 A+B Format (20 分)
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
My code
#include<stdio.h>
#include<stdlib.h>
int main(){
int a,b,c,d;
scanf("%d %d",&a,&b);
c=a+b;
d=abs(c);
if(d<1000){printf("%d",c);}
else if(d<1000000){int m,n;m=c/1000;n=abs(m*1000-c);printf("%d%c%03d",m,',',n);}
else{int m,n,f;m=c/1000000;n=abs(m*1000-c/1000);f=abs(c-n*1000-m*1000000);printf("%d%c%03d%c%03d",m,',',n,',',f);}
return 0;
}
心得体会
输出3位数且不足位用0补上的方法‘%03d’
绝对值函数abs(int a);