注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

东月之神

在单纯的观念里面,生命就容易变得比较深刻!

 
 
 

日志

 
 
关于我

别驻足,梦想要不停追逐,别认输,熬过黑暗才有日出,要记住,成功就在下一步,路很苦,汗水是最美的书!

网易考拉推荐

Total Amount  

2010-06-08 21:52:41|  分类: ACM |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

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

Source

Zhejiang Provincial Programming Contest 2005

 

就一个高精度问题。。

代码:

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
#include <cstdio>
int main()
{
 char s[50];
 int n, i, j, k;
 char a[50], b[50];
 while(scanf("%d", &n) != EOF && n)
 {
  scanf("%s", s);
  j = 0;
  for(i = 0; s[i]; i++)
   if(s[i] >= '0' && s[i] <= '9')
    a[j++] = s[i];
  a[j] = '\0';
  for(i = 1; i < n; i++)
  {
   char t[50];
   scanf("%s", t);   
   k = 0;
   for(j = 0; t[j]; j++)
    if(t[j] >= '0' && t[j] <= '9')
     b[k++] = t[j];
   b[k] = '\0';
   int la = strlen(a), lb = strlen(b);
   int cnt = 0, ans;
   char p[50];
   int kk = 0;
   for(j = la-1, k = lb-1; j >= 0 || k >= 0; j--, k--)
   {
    char a1, b1;
    if(j < 0) a1 = '0';  else a1 = a[j];
    if(k < 0) b1 = '0';  else b1 = b[k];
    ans = (a1 + b1- '0' - '0' + cnt);    
    p[kk++] = (ans % 10) + '0';
    cnt = ans >= 10 ? 1 : 0;
   } 
   if(cnt == 1)p[kk++] = '1';p[kk] = '\0';   
   for(k = strlen(p)-1, j = 0; k >= 0; k--, j++)
    a[j] = p[k];
   a[j] = '\0';
  }  
  string aa;
  k = 0;
  int la = strlen(a);
  for(i = la-1; i >= 0;i--)
  {
   if(i < la-2) k++;
   aa += a[i];
   if(i == la-2) aa += '.';
   if(k % 3 == 0 && k != 0 && i != 0) aa += ',';
  }
  aa += '$';
  for(i = aa.length()-1; i >= 0; i--)
   cout << aa[i];
  cout << endl;
 }
return 0;
}

  评论这张
 
阅读(477)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017