1082 Read Number in Chinese (25 分)

属实头秃_(:з」∠)_

思路:分段处理

1.按:亿级, 万级, 万以下分为三段;
2.对于每一段,设置标志位:flag-前面有非零数,zero-前面有零数, place-万和亿有没有输出过;
3.对于每一段,都当前非零,前方有零有非零数, 则应输出一个零;

从个位至亿位处理,用栈缓存;

测试点3:给定0时输出ling;

#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
    stack<string> ans;
    string Aplace[4] = { "", "Shi", "Bai", "Qian" }, Bplace[3] = { "", "Wan", "Yi" }, digit[10] = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };
    long long N;
    int seg[3];
    cin >> N;
    if( N < 0 )
    {
        cout << "Fu ";
        N *= -1;
    }
    if( !N )
        cout << "ling";
    seg[2] = N / 100000000, seg[1] = N % 100000000 / 10000, seg[0] = N % 10000;
    for( int i = 0, flag = 0, zero = 0; i < 3; ++i )
    {
        if( !seg[i] )
            zero = 1;
        for( int j = 0, place = 0; seg[i]; ++j, seg[i] /= 10 )
            if( seg[i] % 10 )
            {
                if( flag && zero )
                {
                    ans.push("ling");
                    zero = 0;
                }
                else
                    flag = 1;
                if( !place && i )
                {
                    ans.push( Bplace[i] );
                    place = 1;
                }
                if( j )
                    ans.push( Aplace[j] );
                ans.push( digit[ seg[i] % 10 ] );
                zero = 0;
            }
            else
                zero = 1;
    }
    while( !ans.empty() )
    {
        cout << ans.top();
        ans.pop();
        if( !ans.empty() )
            cout << " ";
    }
}

上一篇:1082 射击比赛 PAT (Basic Level)


下一篇:PTA 1082 射击比赛 (c++)