1001. A+B Format (20)
2016-11-22 14:45
309 查看
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 9
Sample Output
-999,991
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 9
Sample Output
-999,991
#include <stdio.h> #include <stdlib.h> int main() { int s[8] = {0}; int a, b, sum, i = 0, t; scanf("%d %d", &a, &b); sum = a + b; if(sum < 0) { printf("-"); sum = sum * (-1); } t = sum; while(sum) //把两数之和按照从低位到高位的顺序放入s中 { s[++i] = sum % 10; sum /= 10; } if(i <= 3) //如果长度不超过3,就不需要加逗号,直接输出 printf("%d", t); else { while(i) { printf("%d", s[i--]); if(i % 3 == 0 && i != 0) //每隔3位加逗号 printf(","); } } return 0; }
相关文章推荐
- 1001. A+B Format (20) C++
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)——C语言
- 1001.A+B Format (20)
- PAT - 甲级 - 1001. A+B Format (20)(字符串处理)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- PAT-A-1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- PAT甲级 1001. A+B Format (20)
- PAT甲级1001. A+B Format (20)解题报告
- 关于‘1001.A+B Format (20)’的解题报告
- PAT---1001. A+B Format (20)
- 1001. A+B Format (20)
- PAT(Advanced Level)1001. A+B Format (20)
- 1001. A+B Format (20)
- PAT (Advanced Level) Practise 1001 A+B Format (20)