A+B Format

题目描述

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).

输入描述:

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.



输出描述:

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.

输入例子:

-1000000 9

输出例子:

-999,991


#include<stdio.h>
#include<math.h>
int str(int a){
    int n=0;
    while(a){
        n++;
        a/=10;
    }
    return n;
}
int main(){
    int a,b,c,m,mm;
    scanf("%d%d",&a,&b);
    m=a+b; 
    if(m<0) mm = -m;
    else mm = m;
    c = str(a+b);
    if(c<=3) printf("%d",m);
    if(c>3&&c<=6){
    printf("%d,%03d",m/1000,mm%1000);
    }
    if(c>6){
        printf("%d,%03d,%03d",m/1000000,mm/1000%1000,mm%1000);
    }
}

这个题没什么特点,反复调整了几次,做完之后感觉并没有利用什么优秀的想法,于是看了一下别人的,好像大家都差不多,意外发现一个博客,实行了一下,好像DEV里不能用,日后有空再看一下。

猜你喜欢

转载自www.cnblogs.com/zhangzimu/p/9181032.html