PAT1001.A+B Format

来源:互联网 发布:正规淘宝兼职平台 编辑:程序博客网 时间:2024/05/19 11:35

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
 
 
ps:与其他网站的第一题确实有不小的区别。需要小小地注意下a+b=0的情况。
 
 
 
 

#include<stdio.h>#include<string.h>

#define N 100int str[N];

int main(){ int a,b; scanf("%d%d",&a,&b); a+=b; if( !a ) { printf("0\n"); return 0; } if( a<0 ) { a=-a; putchar('-'); } int pos=0; while( a ) { str[pos++]=a%10; a/=10; } for(int i=pos-1;i>=0;i--) { printf("%d",str[i]); if( !(i%3) && i ) putchar(','); } puts("");

return 0;}

 
原创粉丝点击