您的位置:首页 > 其它

B-Total Amount (ZOJ 2476)

2014-03-31 15:38 288 查看
Time Limit:2000MS     Memory Limit:65536KB     64bit
IO Format:
%lld & %llu
Submit Status Practice ZOJ
2476

Description

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

题意: 几个数相加..类似高精度加法..

思路: 用数组将每个逗号间的数从符号转换成数字存起来..再用另一个数组求和...

        比赛的时候...数相加进位的时候没有将之前的数也加进去!!!! 比如  su[2]+=(a/100) ..没有写'+'...

        还有输出格式要注意一下...如果整数部分为0 ...则要输出 0...

CODE:

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
char a[20];
int sum[4];

void deal()
{
int k=3;
int s,t;
int len=strlen(a);
memset(sum,0,sizeof(sum));
for(int i=len-1;i>=1;i--)
{
t=1;s=0;
while(a[i]!='.'&&a[i]!=','&&i>=1)
{
s=s+(a[i]-48)*t;
t=t*10;
i--;
}
if(a[i]=='.'||a[i]==','||a[i]=='$')
{
sum[k--]=s;
}
}
}
int main()
{
int t;
int su[4];
while(~scanf("%d",&t),t)
{
getchar();
memset(su,0,sizeof(su));
for(int i=1;i<=t;i++)
{
gets(a);
deal();
int a=sum[3]+su[3];
su[2]+=(a/100);
su[3]=(a%100);
int b=sum[2]+su[2];
su[1]+=(b/1000);
su[2]=(b%1000);
int c=sum[1]+su[1];
su[0]+=(c/1000);
su[1]=(c%1000);
su[0]=sum[0]+su[0];
}
int jh=4;
for(int i=0;i<3;i++)
{
if(su[i]!=0)
{jh=i;break;}
}
printf("$");
for(int i=jh;i<3;i++)
{
if(i==jh)printf("%d",su[i]);
else printf(",%03d",su[i]);
}
if(jh==4) printf("0");
printf(".%02d\n",su[3]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  zoj 14.3.29排位赛