您的位置:首页 > 其它

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

2013-11-28 20:14 441 查看
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
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: