1001. A+B Format (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 9Sample Output
-999,99 1
#include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int a,b; scanf("%d%d",&a,&b); int sum=a+b; int dight[10];//栈用来三位三位的分离 int top=-1; int flag=sum<0?1:0;//存符号 sum=(int)fabs(sum); //sum为0,直接输出 if(sum==0){ printf("%d\n",0); return 0; } //分离 while(sum){ dight[++top]=sum%1000; sum=sum/1000; } if(flag) printf("%c",'-'); if(top==0) printf("%d\n",dight[0]); else{ printf("%d%c",dight[top],','); for(int i=top-1;i>=0;i--) printf("%03d%c",dight[i],i==0?'\n':','); } return 0; }