1001 A+B Format (20)(20 分)

注意余数计算

#include
#include
#include
using namespace std;
int a, b;
int main()
{
    scanf("%d%d", &a, &b);
    int c = a + b;
    if (c < 0)printf("-"), c = -c;
    int cnt = 0;
    char s[30];
    do
    {
        s[cnt++] = c % 10 + '0';
        c /= 10;
    } while (c);
    s[cnt] = '\0';
    int len = strlen(s);
    string ans;
    for (int i = 0; i < len; i++)
    {
        ans += s[i];
        if ((i+1) % 3 == 0 && i != len - 1)ans += ',';
    }
    reverse(ans.begin(), ans.end());
    cout << ans;
    return 0;
}

你可能感兴趣的:(1001 A+B Format (20)(20 分))