Class PTA 1001 A + B Format

The meaning of problems

Given a, b, and a + b is obtained, then every three numbers separated by commas, unless fewer than three.

Thinking

To_string use to convert the data string, and then every three reverse comma

Code

#include <bits/stdc++.h>
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    string s=to_string(a+b);//将数据转换为字符串
    for(int i=(int)s.length()-1;i>=0;i--){//从右往左加逗号
        cout<<s[s.length()-1-i];
        if(s[s.length()-1-i]=='-') continue;//-号后不加逗号
        if(i%3==0&&i) cout<<',';
    }
    return 0;
}
Published 83 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43077261/article/details/103788559