题目链接:http://www.patest.cn/contests/pat-a-practise/1001
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
代码如下:
#include <cstdio> #include <cstring> int main() { int a, b, c; int d[1017]; while(~scanf("%d%d",&a,&b)) { int flag = 0; c = a+b; int l = 0; if(c == 0) { printf("0\n"); continue; } if(c < 0) { flag = 1; c = -c; } for(int i = 0; ; i++) { int tt = c%10; c/=10; //printf("c::%d\n",c); d[l++] = tt; //printf("tt::%d\n",tt); if(c == 0) break; } int k = 0; if(flag) printf("-"); int cont = l; for(int i = l-1; i >= 0; i--) { printf("%d",d[i]); cont--; if(cont%3==0 && cont!=0 && i!=0) printf(","); } printf("\n"); } return 0; } /* -1000000 9 -1000000 -1000000 */
1001. A+B Format (20) (数学啊 ZJU_PAT)
原文地址:http://blog.csdn.net/u012860063/article/details/41554115