zoukankan      html  css  js  c++  java
  • PAT-1001 A+B Format (20 分) 注意零的特例

    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 Specification:

    Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.

    Output Specification:

    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
    

    就是计算a+b然后输出的水题,要注意符号判断,如果为0的话就输出一个0就可以了.
    做任何题都要小心结果会不会就是一个0,还有一个0不算是前导0.

    #include <iostream>
    #include <bits/stdc++.h>
    #define de(x) cout<<#x<<" "<<(x)<<endl
    using namespace std;
    
    int a[10];
    int cur=0;
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        int sum=n+m;
        bool sign_flag=true;
        bool zero_flag=false;
        if(sum==0)zero_flag=true;
        if(sum<0)sign_flag=false,sum=-sum;///
        while(sum>0)
        {
            a[cur++]=sum%10;
            sum/=10;
        }
        if(!sign_flag)printf("-");
        int cnt=0;
        for(int i=cur-1;i>=0;i--)
        {
            printf("%d",a[i]);
            cnt++;
            if(i%3==0&&i!=0)
            {
                printf(",");
            }
        }
        if(zero_flag)printf("0");
        printf("
    ");
    
        return 0;
    }
    
    
  • 相关阅读:
    即将到来的Android N,将具备这些新特性
    刚挣钱的程序猿同学该怎样花钱?
    Swift函数
    MVC
    css3硬件加速
    node+mongoDB+express项目需求解释
    柯里化
    web安全
    缓存机制详解
    nodejs --- crypto实现加密(转)
  • 原文地址:https://www.cnblogs.com/Tony100K/p/11757806.html
Copyright © 2011-2022 走看看