zoukankan      html  css  js  c++  java
  • ZOJ 2476 Total Amount

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2476

    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

     1 #include<stdio.h>
     2 #include<cstring>.
     3 #include<iostream>
     4 using namespace std;
     5 long long f(int n)
     6 {
     7     long long res=1;
     8     while(n--)
     9         res*=10;
    10     return res;
    11 }
    12 char ans[20];
    13 void change(long long a)
    14 {
    15     int i,j;
    16     if(a==0)
    17     {
    18         cout<<"0";
    19         return;
    20     }
    21     for(i=0,j=1;i<20;i++,j++)
    22     {
    23         if(a<1)break;
    24         ans[i]='0'+(a%10);
    25         a/=10;
    26         if(j%3==0)
    27         {
    28             i++;
    29             ans[i]=',';
    30         }
    31     }
    32     int len=i;
    33     for(i=len-1;i>=0;i--)
    34     {
    35         if(i==len-1&&ans[i]==',');
    36         else cout<<ans[i];
    37     }
    38 }
    39 int main()
    40 {
    41     int n,len,i,j;
    42     long long resz;
    43     int resf;
    44     char s[20];
    45     while(cin>>n&&n)
    46     {
    47         resz=0;
    48         resf=0;
    49         while(n--)
    50         {
    51             cin>>s;
    52             len=strlen(s);
    53             resf+=s[len-1]-'0';
    54             resf+=(s[len-2]-'0')*10;
    55             while(resf>=100)
    56             {
    57                 resz+=1;
    58                 resf-=100;
    59             }
    60             for(i=len-3,j=0;i>=0;i--)
    61             {
    62                 if(s[i]>='0'&&s[i]<='9')
    63                 {
    64                     resz+=(s[i]-'0')*f(j);
    65                     j++;
    66                 }
    67             }
    68         }
    69         cout<<"$";
    70         change(resz);
    71         cout<<".";
    72         if(resf<10)cout<<"0"<<resf<<endl;
    73         else cout<<resf<<endl;
    74     }
    75     return 0;
    76 }
  • 相关阅读:
    一个挺好用的自己写的小插件(用与把一般的图片转换成预制)——UNITY3D
    ios网络学习------4 UIWebView的加载本地数据的三种方式
    ios网络学习------6 json格式数据的请求处理
    OC 解决NSArray、NSDictionary直接打印中文出现乱码的问题
    网络数据的XML解析
    iOS对象序列化
    iOS数据存取和对象序列化
    iOS NSDictionary、NSData、JSON数据类型相互转换
    IOS四种保存数据的方式
    ios开发值json数据文件的存取
  • 原文地址:https://www.cnblogs.com/Annetree/p/5682286.html
Copyright © 2011-2022 走看看