zoj 2476 Total Amount(模擬問題、詳細処理が特に多い)

5561 ワード

1、http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2476
2、タイトルの大意:
n個の大きな数を与えて、合計を求めて、最後の出力形式は後から前の3つに句読点を加えます.
3、コンマの処理に注意すればいいです.最初に多く出たのは最大4桁で、多く出た桁数を処理して、コンマをつけてください.
タイトル:
Total Amount
           
                           
Time Limit:2 Seconds                                   
メモリLimit:65536 KB                           
           
           
Gven a list of montary amounts in a standard format,please calculate the   total amount.
We define the formas 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 follwing digits.
4.The digits to the left of the decimal point are separated into groups of   three bycommmas(a group of one or two digits may appar 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ライン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("
"); } 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
",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("
"); } else { printf("%d",anss); for(int i=1;i<maxx;i++) printf("%c",a[i]); printf("
"); } } } 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 */