zoukankan      html  css  js  c++  java
  • PAT甲级真题打卡:1001.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).

    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

    参考代码:(C++)

    #include<stdio.h>

    using namespace std;

     

    int main(void){

    int a,b;

    while(scanf("%d%d",&a,&b)!=EOF){

    int c = a+b;

    if(c<0){

    c=-c;

    printf("-");

    }

    if(c>=1000000)

    printf("%d,%03d,%03d ",c/1000000,(c/1000)%1000,c%1000);

    else if(c>=1000)

    printf("%d,%03d ",c/1000,c%1000);

    else

    printf("%d ",c);

    }

    return 0;

    }

     

    分析:

    对于负数,可以先判断a+b的结果是否为负,若结果为负数,则先输出负号『-』

    由题目a,b范围,可知a+b的结果的绝对值小于10000000,所以对于大于1000000的部分c/1000000即可。

  • 相关阅读:
    二十四种设计模式:观察者模式(Observer Pattern)
    二十四种设计模式:工厂方法模式(Factory Method Pattern)
    Java迷题:等于,还是不等于?
    UCenter创始人、Discuz!创始人、管理员账号的认知(转)
    Internet Explorer 无法启用 JavaScript 怎么办?
    IIS配置PHP环境(快速最新版)(转载+自创)
    零基础学php的自学
    PHP实体层基础类
    PHP数据库链接类(PDO+Access)
    C# Memcached 缓存
  • 原文地址:https://www.cnblogs.com/ton2018/p/8931575.html
Copyright © 2011-2022 走看看