ZOJ Problem Set - 2476 Total Amount

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    




分析:

题意:

给出若干按照一定形式表示的货币数字($11,111,111.10),要求求出它们的和。有多组测试数据。



题目看起来很简单。就是用字符串处理。但很多细节只有自己去敲代码才知道怎么处理。字符串转换成数字的求和。需要耐心和扎实的编程功底。



ac代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
string sa,sb,t;
int main()
{
    int pa,pb,pc;
    int n,i,j;
    map<char ,int> m;
    m['0']=0;//存在map容器其实就相当于if-else语句
    m['1']=1;
    m['2']=2;
    m['3']=3;
    m['4']=4;
    m['5']=5;
    m['6']=6;
    m['7']=7;
    m['8']=8;
    m['9']=9;
    map<int,char> mm;
    mm[0]='0';
    mm[1]='1';
    mm[2]='2';
    mm[3]='3';
    mm[4]='4';
    mm[5]='5';
    mm[6]='6';
    mm[7]='7';
    mm[8]='8';
    mm[9]='9';
    bool flag=0;
    while(scanf("%d",&n)&&n)
    {
        for(i=0;i<n;i++)
        {
            cin>>sb;
            sb.erase(0,1);
            t="";//定义一个空串
            for(j=0;j<sb.size();j++)
            {
                if(sb[j]!=',') t+=sb[j];
            }
            sb=t;
            reverse(sb.begin(),sb.end());
            sb.erase(2,1);//删除"."
            if(i==0) sa=sb;
            else
            {
                flag=0;//进位每次都置零
                if(sa.size()<sb.size())
                {
                    t=sa;sa=sb;sb=t;
                }
                for(j=0;j<sa.size();j++)
                {
                    pa=m[sa[j]];
                    if(j>sb.size()) pb=0;
                    else pb=m[sb[j]];
                    pc=pa+pb+flag;
                    if(pc>9)
                    {
                        pc=pc-10;
                        flag=1;
                    }
                    else flag=0;
                    sa[j]=mm[pc];//将和pc转换为字符
                }
                if(flag) sa+="1";//把字符串相加的结果存在sa
            }
        }
        t="";
        for(i=0;i<sa.size();i++)
        {
           t+=sa[i];
           if(i==1) t+=".";
           if((i-1)%3==0&&i!=1&&i!=sa.size()-1)
           t+=",";
        }
        sa=t;
        printf("$");
        reverse(sa.begin(),sa.end());
        cout<<sa<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值