码迷,mamicode.com
首页 > 其他好文 > 详细

A+B Format

时间:2018-06-14 11:40:11      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:blank   优秀   ESS   输入   format   描述   contains   ++   href   

题目描述

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

标签:blank   优秀   ESS   输入   format   描述   contains   ++   href   

原文地址:https://www.cnblogs.com/zhangzimu/p/9181032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!