zoj 2476

Total Amount

Time Limit: 1 Second      Memory Limit: 32768 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

 

//2476
#include <iostream>
#include <string>
using namespace std;


int main()
{
 int n ;
 while(cin>>n&&n!=0)
 {
  string str;
  n--;
  cin>>str;
  string sum = str;
  sum = sum.substr(1,sum.length());
  while(n--)
  {
   cin>>str;
   str = str.substr(1,str.length());
   if(sum.length()<str.length())
    swap(sum,str);

   int f = 0;
   int i , j;
   for(i = sum.length()-1, j = str.length()-1 ; j>=0 ; i--,j--)
   {
    if(str.at(j)!='.'&&str.at(j)!=',')
    {
     int t = sum.at(i) + str.at(j) + f - '0'*2;
     char tt;
     if( t>=10)
     {
      f = 1;
      tt = char(t%10+'0');
     }
     else
     {
      f = 0;
      tt = char(t+'0');
     }
     sum[i] = tt;
    }
   }

   while(f!=0&&i>=0)
   {
    if(sum.at(i)!='.'&&sum.at(i)!=',')
    {
     int t = sum.at(i) + f - '0';
     char tt;
     if( t>=10)
     {
      f = 1;
      tt = char(t%10+'0');
     }
     else
     {
      f = 0;
      tt = char(t+'0');
     }
     sum[i] = tt;     
    }
    i--;
   }

   if(f!=0&&i==-1)
    sum = char(f+'0') + sum;
  }
  sum = '$' + sum;
  cout<<sum<<endl;  
 }
 return 0;
}

 

================================================

 

测试数据

 

2
$9,099,802.11
$23,234,999,999,999,001.23

2
$998.09
$1.00

2
$1.00
$1.00

 

3
$0.22
$39,178,437,598,718,923.78
$485,983,720.00

2
$999.00
$1.00

2
$872.00
$23.01

2
$0.01
$0.99

2
$0.01
$999.99

 

10

$999,234,999,999,999,001.23
$999,234,999,999,999,001.23
$999,234,999,999,999,001.23
$999,234,999,999,999,001.23
$999,234,999,999,999,001.23
$999,234,999,999,999,001.23
$999,234,999,999,999,001.23
$999,234,999,999,999,001.23
$999,234,999,999,999,001.23
$999,234,999,999,999,001.23

你可能感兴趣的:(String,Integer,less,input,each,output)