1001 A+B Format (20分)

题意

计算 a+b 并以标准格式输出总和——也就是说,从最低位开始每隔三位数加进一个逗号(千位分隔符),如果结果少于四位则不需添加。

const int N=1010;
int a,b;

int main()
{
    cin>>a>>b;

    int sum=a+b;
    string s=to_string(sum);

    int st=0;
    if(s[0] == '-') st=1;

    string res="";
    int cnt=0;
    for(int i=s.size()-1;i>=st;i--)
    {
        res+=s[i];
        cnt++;
        if(cnt % 3 == 0 && i != st) res+=',';
    }

    if(st != 0) res+='-';
    reverse(res.begin(),res.end());
    cout<<res<<endl;
    //system("pause");
}
原文地址:https://www.cnblogs.com/fxh0707/p/14221727.html