1001. A+B Format (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.
Output
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 9Sample Output
-999,991
分析题意:计算sum=a+b的和,并以千分位的形式输出,先判断sum的正负数,如果是负数,先输出‘-’,对sum取绝对值,再判读sum的取值范围
AC解题代码:
#include<cstdio> #include<stdio.h> using namespace std; int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF) { int sum=a+b; if(sum<0) { printf("-"); sum=-sum; } if(sum<1000) printf("%d\n",sum); else if(sum<1000000) printf("%d,%03d\n",sum/1000,sum%1000); else printf("%d,%03d,%03d\n",sum/1000000,(sum%1000000)/1000,sum%1000); } return 0; }
本文介绍了一个简单的编程问题,即计算两个整数a和b的和,并以标准的千分位格式输出结果。文章提供了完整的AC代码实现,展示了如何根据不同数值范围进行格式化输出。
1万+

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



