zoj 2476 Total Amount(模拟题,细节处理特别多)

1、http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2476

2、题目大意:

给定n个大数,求和,最终输出格式是从后往前数每三个加一个标点

3、注意逗号的处理即可,注意最开始的多出来的最多有4位,得处理多出来的位数,加几个逗号

题目:

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

 

4、AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char str[10005][20];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        int maxx=-100,idx;
        for(int i=0; i<n; i++)
        {
            scanf("%s",str[i]);
            int ll=strlen(str[i]);
            if(maxx<ll)
            {
                maxx=strlen(str[i]);
                idx=i;
            }
        }
        char b[20];
        for(int i=0; i<n; i++)
        {
            int len=strlen(str[i]);
            if(len==maxx)
                continue;
            else
            {
                b[0]='$';
                for(int j=1; j<maxx-len; j++)
                    b[j]='0';
                int k=1;
                b[maxx-len]='0';
                for(int j=maxx-len+1; j<maxx; j++)
                    b[j]=str[i][k],k++;
                b[maxx]='\0';
                strcpy(str[i],b);
            }
        }
        char a[20];
        int ans=0;
        for(int i=maxx-1; i>=0; i--)
        {
            if(str[idx][i]=='.' || str[idx][i]==',' || str[idx][i]=='$')
            {
                a[i]=str[idx][i];
                continue;
            }
            int count=0;
            for(int j=0; j<n; j++)
            {
                count+=(str[j][i]-'0');
            }
            count+=ans;
            ans=count/10;
            a[i]=count%10+'0';
        }
        if(ans==0)
        {
            for(int i=0; i<maxx; i++)
                printf("%c",a[i]);
            printf("\n");
        }
        else
        {
            printf("%c",a[0]);
            //printf("%d",ans);
            int res=0,anss=ans;
            int cc[20],c[20],kk=0;
            while(ans)
            {
                res++;
                c[kk++]=ans%10;
                ans/=10;
            }
            //printf("res=%d\n",res);
            int pp=0;
            for(int p=kk-1; p>=0; p--)
            {
                cc[pp++]=c[p];
            }
            int r=-1;
            for(int ii=0; ii<maxx; ii++)
            {
                if(a[ii]==',' || a[ii]=='.')
                {
                    r=ii;
                    break;
                }
            }
            int l=kk+r-1;
            if(l>3)
            {
                int ll=l%3;
                for(int p=0; p<ll; p++)
                    printf("%d",cc[p]);
                printf(",");
                int count=0;
                for(int p=ll;p<kk;p++)
                {
                    if(count==3)
                    printf(","),count=0;
                    else
                    {
                        printf("%d",cc[p]);
                        count++;
                    }
                }
                for(int p=1;p<r;p++)
                {
                    if(count==3)
                    printf(","),count=0;
                    else
                    printf("%c",a[p]),count++;
                }
                for(int i=r; i<maxx; i++)
                printf("%c",a[i]);
            printf("\n");
            }
            else
            {
                printf("%d",anss);
                for(int i=1;i<maxx;i++)
                printf("%c",a[i]);
                printf("\n");
            }

        }

    }
    return 0;
}
/*
2
$1,234,567.89
$19,234,567.89
2
$567.89
$543.21

2
$567,124.89
$543,124.21

2
$999.99
$1.01
3
$564,124.89
$543,124.21
$3,124.21
2
$20,000,000.99
$20,023,000.99
20
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
$999,999,999.99
*/


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值