PATA1001A+BFormat

  • We learned here mainly to the digital storage array, using the output reverse addition modulo 10 plus 10
while(sum)
{
    num[len++] = sum % 10;
    sum /= 10;
}
  • Then every three output a comma, because it is the reverse output can be every 3 remainder is 0 when printing a comma.
    Reference Code:
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cstdlib>
int num[10];

int main()
{
    int a, b, sum;
    scanf("%d %d", &a, &b);
    sum = a + b;
    if (sum < 0)//如果为负数首先输出负号,然后变正后再处理。
    {
        printf("-");
        sum = -sum;
    }

    int len = 0;
    if (sum == 0) num[len++] = 0;
    while (sum)
    {
        num[len++] = sum % 10;
        sum /= 10;
    }

    for (int i = len - 1; i >= 0; i--)
    {
        printf("%d", num[i]);
        if (i > 0 && i % 3 == 0)printf(",");
    }

    system("pause");
    return 0;

}

Guess you like

Origin www.cnblogs.com/tsruixi/p/11232721.html