关闭

1001. A+B Format (20)

58人阅读 评论(0) 收藏 举报
分类:

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

思路:将a+b的和存入char型数组c[N],再输出

#include<stdio.h>
int Trans(int n,char c[]);
int main(){
    int a,b,len,sum,i,r,count=0;
    char c[10];
    scanf("%d %d",&a,&b);
    sum=a+b;
    len=Trans(sum,c);
    if(sum<0){printf("-");sum=-sum;}
    r=len%3;
    for(i=len-1;i>=0;i--){
        printf("%c",c[i]);
        if(r>0){
            r--;
            if(r==0&&len>=3)printf(",");
            continue;
        }
        count++;
        if(count%3==0&&i!=0)printf(",");
    }
}
int Trans(int n,char c[]){
    int i=0,m;
    m=n<0?(-n):n;
    while(m!=0){
        c[i++]=m%10+'0';
        m/=10;
    }
    if(n==0){
        c[0]='0';
        i=1;
    }
    return i;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:42919次
    • 积分:2793
    • 等级:
    • 排名:第12903名
    • 原创:249篇
    • 转载:6篇
    • 译文:0篇
    • 评论:8条
    最新评论