1001 A+B Format (20分)

题目详情:

Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). ( 计算a+b的和并且按照标准要求输出————当|a+b|>999时,输出必须三位一组并且用逗号分开)。

输出示例:

算法流程:

代码:

#include <stdio.h>
#include <stdlib.h>
#include <cmath>
int main()
{
    int a,b;
    int len=0,term,res[7];
    scanf("%d%d",&a,&b);
    term=a+b;
    if(term<0)
    {
        printf("-");
    }
    term = abs(term);
    do{
        res[len] = term%10;
        term/=10;
        len++;
    }
    while(term!=0);
    len-=1;
    if(len<3)
    {
        for(int i=len;i>=0;i--)
        {
            printf("%d",res[i]);
        }
    }
    else
    {
        for(int i=len;i>=0;i--)
        {
            if((i+1)%3==0 && i!=len)
            {
                printf(",");
            }
            printf("%d",res[i]);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/kadcyh/p/14332533.html