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
乍一看以为是字符串处理,不过仔细一看,范围只有到1000000 ,每三位一个逗号,三种情况if判断下就完事了,题目仔细分析很有必要.
// // main.cpp // PAT // // Created by hqr on 14-2-7. // Copyright (c) 2014年 hqr. All rights reserved. // #include <iostream> using namespace std; int nA,nB,nC; int main(int argc, const char * argv[]) { // insert code here... cin >> nA >> nB; nC = nA + nB; if(nC < 0) { nC = -nC; cout<<"-"; } if(nC>=1000000) { printf("%d,%03d,%03d\n",nC/1000000,(nC%1000000)/1000,nC%1000); }else if(nC>=1000){ printf("%d,%03d\n",nC/1000,nC%1000); }else{ cout << nC << endl; } return 0; }