A+B Format

Posted zhangzimu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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里不能用,日后有空再看一下。

 






以上是关于A+B Format的主要内容,如果未能解决你的问题,请参考以下文章

A+B Format (20)

片段 A 的列表视图中的片段 B 中的新列表视图,单击 A 的列表项

1001. A+B Format (20)

apriori片段代码

PAT甲级 1001. A+B Format (20)

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段