A+B Format

来源:互联网 发布:fc2live直播域名 编辑:程序博客网 时间:2024/06/05 22:45

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
思路:利用一个字符数组2元素和的最小位到最高位,然后倒序输出,每次存了3个数字的时候,要加一个逗号。


#include<stdio.h>int main(){  int a,b,m,i=0,j=0;  //m记录个位数  char out[20];  int flag=0;  scanf("%d%d",&a,&b);  int sum=a+b;  if(sum<0) {flag=1; sum=-sum;}  while(sum){    m=sum%10;    sum=sum/10;    out[j++]= m +'0';    i++;  //i记录位数    if(i%3==0) out[j++]=',';  }  if(flag)  printf("-");  if(out[j-1]==',') j--; //如果第一位是',',那么就不输出  for(;j>0;j--){    printf("%c",out[j-1]); }  return 0;}