PAT. advance level. T.1001. A+B format

来源:互联网 发布:淘宝助理如何删除宝贝 编辑:程序博客网 时间:2024/06/01 09:18
#include<stdio.h>main(){    long a,b,c,n,z,i,temp1,sum[10],temp2,temp3,temp4,temp5,temp6,test;    c = 0;    n = 0;    z = 1;    i = 0;    temp1 = 1;    temp2 = 0;    temp3 = 0;    temp4 = 0;    temp5 = 0;    temp6 = 0;    scanf("%ld %ld",&a,&b);    c = (a + b);    test = c;        if (c<0)    {        printf("-");        c = -c;        n = c;    }else {        n = c;    }    while (c > 9){        c /= 10;        z = z * 10;        temp6++;    }    z = z * 10;    temp6++;    temp2 = temp6;    while (n>9){        i = n % 10;        n /= 10;        sum[temp1] = i;        temp1++;            }    sum[temp1] = n;    temp3 = temp1 % 3;    for (temp4=temp1;temp4>=1;temp4--){        if (test == 0){            printf("0");            break;        }         printf("%ld",sum[temp4]);        if (((temp1-temp4+1) == temp3 )&&(temp3!=0)&&(temp1>3)){         printf(",");         temp5 = 0;         continue;        }        temp5++;        if ((temp5 == 3)&&(temp4!=1)){           printf(",");        }    }    return 0;}

先判断相加之和的正负,为0和负数的时候划分出来。
用数组将每一个数拆分出来,然后逆序,先%3,可以得出最前有几个数字就用逗号。最后输出。

0 0