• PAT1001


    1001. A+B Format (20)

    时间限制

    400 ms

    内存限制

    32000 kB

    代码长度限制

    16000 B

    判题程序

    Standard

    作者

    CHEN, Yue

    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
    AC源码:
    #include 
    
    using namespace std;
    
    int abs(int x)
    {
        return x >= 0 ? x : -x;
    }
    
    int main()
    {
        int a;
        int b;
        int buf[4];
    
        while(scanf("%d %d", &a, &b) != EOF)
        {
            int rst = a + b;
            bool mark;
            if(rst >= 0)
            {
                mark = true;
            }
            else
            {
                mark = false;
            }
    
            int cnt = 0;
    
            rst = abs(rst);
    
            if(rst == 0)
            {
                printf("0\n");
                continue;
            }
    
            while(rst != 0)
            {
                buf[cnt++] = rst % 1000;
                rst /= 1000;
            }
    
            if(!mark)
            {
                printf("-");
            }
    
            for(int i = cnt - 1; i >= 0; i--)
            {
                if(i == cnt - 1)
                {
                    printf("%d", buf[i]);
                }
                else
                {
                    printf(",%03d", buf[i]);
                }
            }
    
            printf("\n");
        }
    
        return 0;
    }
     
    提交结果:

    image

    点评:

    刚开始没有考虑结果0,所以只有14分,加了之后15分,通过用例1000 1发现输出格式问题,改正,满分A过。

  • 相关阅读:
    web.xml文件详解
    SQLSERVER dbo解释
    sqlserver BULK INSERT
    google 基站定位api
    Sqlserver中Select和Set区别
    SQL Server优化50法
    ibatis常用16条SQL
    面向对象 -- 三大特性之继承 补充 抽象类 接口类
    面向对象 -- 三大特性之继承
    面向对象 -- 类的组合
  • 原文地址:https://www.cnblogs.com/panpei/p/2983630.html
Copyright © 2020-2023  润新知