PAT Advanced 1001 A+B Format (20 分)

PAT Advanced 1001 A+B Format (20 分)
强烈推介IDEA2021.1.3破解激活,IntelliJ IDEA 注册码,2021.1.3IDEA 激活码  

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说PAT Advanced 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 −. 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

 

#include<iostream>
#include<stack>
using namespace std;
int main() {
    long a,b;
    cin>>a>>b;
    long res=a+b;
    if(res==0){
        cout<<0;
        system("pause");
        return 0;
    }
    bool sign;
    int m=0;
    if(res<0) {
        sign=false;
        res=-res;
    }
    else sign=true;
    stack<char> sta;
    while(res!=0){
        m++;
        sta.push(res%10+'0');
        res/=10;
        if(m%3==0) sta.push(',');
    }
    if(sta.top()==',') sta.pop();
    if(!sign) cout<<"-";
    while(!sta.empty()){
        cout<<sta.top();
        sta.pop();
    }
    system("pause");
    return 0;
}

 

转载于:https://www.cnblogs.com/littlepage/p/11300842.html

本文来源weixin_30595035,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/29524

发表评论