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 \(−10^6≤a,b≤10^6\). 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

题目大意

两个输入量相减按照规定格式输出结果,每三位用逗号分隔

思路

int相加后转换成字符串加标点然后输出

代码

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main( ){
    int a,b;
    string o;
    scanf("%d%d",&a,&b);
    o=to_string(a+b);
    int flag=0;
    for(int i=o.size()-1;i>0;i--){
        if(++flag%3==0&&o[i-1]!='-'){
            o.insert(i,",");
        }
    }
    cout<<o<<endl;
    return 0;}

posted @ 2021-02-05 22:20  ATubby  阅读(41)  评论(0)    收藏  举报