PAT A 1001 A+B Format

#include <stdio.h>
#include <math.h>
#define N 100
int main()
{
    int a,b,c,k,i=0,j,tag=0;
    int A[N],B[N];
    scanf("%d %d",&a,&b);
    c=a+b;
    if(c==0)
    {
        printf("%d",c);
    }
    else
    {
        if(c<0)
        {
            tag=1;
        }
        c=abs(c);
        while(c>0)
        {
            A[i++]=c%10;
            c=c/10;
        }
        if(tag==1)
        {
            printf("-");
        }
        for(j=0; j<i; j++)
        {
            B[j]=A[i-1-j];
        }
        for(k=0; k<j; k++)
        {
            printf("%d",B[k]);
            if((j-k-1)%3==0&&k<j-3)
            {
                printf(",");
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41168353/article/details/79720991
今日推荐