1001 A+B Format (20)
1001 A+B Format (20)(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
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<iostream> using namespace std; int main() { int a,b,c; cin >> a >>b; c = a + b; if(c < 0) { cout <<"-"; c = -c; } int num[10];//用来存放c中的每一位 int len = 0; if(c == 0) { num[0] = 0; len = 1; } while(c != 0) { num[len++] = c%10; c = c/10; } for(int i = len -1;i >= 0;i--) { cout << num[i]; if(i == 6 || i == 3) { cout << ","; } } return 0; }
作者:meiqin
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。