ZOJ 2476 Total Amount A的好辛苦

点击打开链接
Total Amount

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Given a list of monetary amounts in a standard format, please calculate the total amount.

We define the format as follows:

1. The amount starts with '$'.

2. The amount could have a leading '0' if and only if it is less then 1.

3. The amount ends with a decimal point and exactly 2 following digits.

4. The digits to the left of the decimal point are separated into groups of three by commas (a group of one or two digits may appear on the left).


Input

The input consists of multiple tests. The first line of each test contains an integer N (1 <= N <= 10000) which indicates the number of amounts. The next N lines contain N amounts. All amounts and the total amount are between $0.00 and $20,000,000.00, inclusive. N=0 denotes the end of input.


Output

For each input test, output the total amount.


Sample Input

2
$1,234,567.89
$9,876,543.21
3
$0.01
$0.10
$1.00
0


Sample Output

$11,111,111.10
$1.11



Author:  ZHANG, Zheng
Source:  Zhejiang Provincial Programming Contest 2005

言语已经无法表达我内心激动的心情了,终于过了这道题,真是不容易这道题WA了八次,真是高兴。
此题也不是很难,就是处理的时候细节多一些,尤其是处理逗号的时候,首先我是将字符串存到整型数组里面,然后依据大数相加的原理算出结果来,最后处理输出问题,一开始初始化的时候没处理好,错了好多次。sad。

#include
#include
char s[10000][107],ss[10000][107];
int  z[107];
int main()
{
    int n,a;
    while(scanf("%d",&n),n)
    {
        int max=-1,len;
        memset(z,0,sizeof(z));
        for(int i=1; i<=n; i++)
        {
            memset(s,0,sizeof(s));//一开始把这两个初始化写到for循环外面了,导致错了很多次
            memset(ss,0,sizeof(ss));
            scanf("%s",s[i]);
            a=0;
            len=strlen(s[i]);
            if(len>max)max=len;
            for(int j=len-1; j>=0; j--)//将字符串反向存入新的数组中
                ss[i][a++]=s[i][j];
            a=0;
            for(int j=0; j='0'&&ss[i][j]<='9')
                    z[a++]+=ss[i][j]-'0';
            }
        }
        for(int i=0; i=10)
            {
                while(z[i]>=10)
                {
                    z[i]-=10;
                    z[i+1]++;
                }
            }
        printf("$");
        int bb;
        for(int i=max-1; i>=0; i--)//去掉前导零,求出其真正长度
            if(z[i]!=0)
            {
                bb=i;
                break;
            }
        len=bb+1;
        if(len==1)printf("0.0%d\n",z[0]);
        else if(len==2)printf("0.%d%d\n",z[1],z[0]);
        else if(len>2&&len<=5)
        {
            for(int i=len-1;i>=2;i--)
                printf("%d",z[i]);
            printf(".%d%d\n",z[1],z[0]);
        }
        else
        {
            int lenn=len-2,flag=0,c=0,d=0;
            int mol=lenn%3;
            if(mol==0){flag=1;}
            for(int i=len-1;i>=2;i--)
            {
                c++;
                if(flag)d++;
                printf("%d",z[i]);
                if(c==mol){printf(",");flag=1;}
                if(d!=0&&d%3==0&&i!=2)printf(",");//if里面的这三个为必须条件,缺一不可
            }
            printf(".%d%d\n",z[1],z[0]);
        }
    }
    return 0;
}


你可能感兴趣的:(数据结构)