PAT A1001 A+B Format (20 分)

AC代码

#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 11;
int main() {
    #ifdef ONLINE_JUDGE
    #else
        freopen("1.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    long long a, b;
    scanf("%lld %lld", &a, &b);
    long long sum = a + b;
    int ans[maxn];
    int num = 0;
    if(sum < 0) {
        printf("-");
        sum = -sum;
    }
    do {
        ans[num++] = sum % 10;
        sum /= 10;
    } while(sum != 0);
    //reverse(ans, ans+num);2
//    for(int i = num - 1; i >= 0; i--) printf("%d", ans[i]);
    for(int i = num - 1, t = 1; i >= 0; i--,t++) {
        printf("%d", ans[i]);
        if(i > 0 && i % 3 == 0) {
                printf(",");
        }
        t++;
    }
    return 0;
}
posted @ 2019-08-14 16:24  哨音  阅读(141)  评论(0编辑  收藏  举报