PAT (Advanced Level) 1001. A+B Format (20)

简单题。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;

int a,b,tot,cnt;
int u[20];
char ans[100];

int main()
{
    while(~scanf("%d%d",&a,&b))
    {
        a=a+b;
        if(a==0)
        {
            printf("0
");
            continue;
        }
        int tmp=abs(a);
        tot=0,cnt=0;
        while(tmp) u[tot++]=tmp%10,tmp=tmp/10;

        for(int i=0; i<tot; i++)
        {
            ans[cnt++]=u[i]+'0';
            if((i+1)%3==0&&tot-i!=1) ans[cnt++]=',';
        }
        if(a<0) ans[cnt++]='-';
        for(int i=cnt-1; i>=0; i--) printf("%c",ans[i]);
        printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/5495934.html