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);
本文介绍了一段C语言代码,用于计算两个整数之和并以标准格式输出,即把千位以上的数字每三位用逗号隔开。作者分享了如何使用`%03d`格式化输出以及绝对值函数`abs()`的技巧。通过示例和心得,读者可以掌握如何处理不足四位的数字问题。
7915

被折叠的 条评论
为什么被折叠?



