zoukankan      html  css  js  c++  java
  • ZOJ 2476 Total Amount 字符串模拟

    - Total Amount
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

    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

     
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e+5+5;
    char a[maxn];
    int c[maxn],d[maxn],ans[maxn];
    int main()
    {
        int T;
        while(~scanf("%d",&T) && T)
        {
            memset(ans,0,sizeof(ans));
            memset(d,0,sizeof(d));
            int index,cnt=0;
            while(T--)
            {
                memset(c,0,sizeof(c));
                memset(a,0,sizeof(a));
                scanf("%s",a);
                int k=0;
                for(int i=0; i<strlen(a); i++)
                {
                    if(isdigit(a[strlen(a)-i-1]))
                        c[k++]=a[strlen(a)-i-1]-'0';
                }
                for(int i=0; i<100; i++)
                {
                    d[i]+=c[i];
                    d[i+1]+=d[i]/10;
                    d[i]%=10;
                }
            }
            for(int i=100; i>=0; i--)
            {
                if(d[i]!=0)
                {
                    index=i;
                    break;
                }
            }
            for(int i=index; i>=2; i--)
                ans[cnt++]=d[i];
            printf("$");
            if(cnt==0)printf("0");
            else
            {
                for(int i=0; i<cnt; i++)
                {
                    printf("%d",ans[i]);
                    if((cnt-i-1)%3==0 && i!=cnt-1)
                        printf(",");
                }
            }
            printf(".%d%d
    ",d[1],d[0]);
        }
        return 0;
    }
  • 相关阅读:
    算法——排序方法总结(冒泡,快速,直接,希尔,堆,归并排序)
    C++函数调用之——值传递、指针传递、引用传递
    STM32(13)——SPI
    STM32(12)——CAN
    STM32(11)——DMA
    STM32(10)——窗口看门狗
    STM32(9)——通用定时器作为输入捕捉
    SRM32(8)——ADC和DAC
    WPF从入门到放弃系列第二章 XAML
    WPF从入门到放弃系列第一章 初识WPF
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9494636.html
Copyright © 2011-2022 走看看