美文网首页
1082 Read Number in Chinese (字符串

1082 Read Number in Chinese (字符串

作者: virgilshi | 来源:发表于2018-10-01 16:57 被阅读0次
    1082 Read Number in Chinese (25 分)

    Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fu first if it is negative. For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu. Note: zero (ling) must be handled correctly according to the Chinese tradition. For example, 100800 is yi Shi Wan ling ba Bai.

    Input Specification:
    Each input file contains one test case, which gives an integer with no more than 9 digits.

    Output Specification:

    For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.

    Sample Input 1:
    -123456789
    
    Sample Output 1:
    Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
    
    Sample Input 2:
    100800
    
    Sample Output 2:
    yi Shi Wan ling ba Bai
    
    分析

    本题的难点在于如何处理零,自己的代码在处理零时总存在测试点通不过,参考博客[1],现在对博客的解法做一点个人的理解分析。解法使用zero标记是否在下一次碰到非零元素时是否输出合理的零(i.e.连续0或者单独一个0,按照中文读法输出一个ling),当zero=false时表示还未遇见0或者上次的0事件(i.e.连续0或者单独0)已经处理完毕,当zero=true时,下次遇到非零元素时需要处理0,即先输出ling再输出非零元素的中文读法,或者一直到最后一位(个位)依然未出现0元素,则不输出ling,zero=true在本次标记中无效。

    #include <iostream>
    #include <algorithm>
    #include<vector>
    #include<cmath>
    using namespace std;
    string kv[]= {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
    string bit[]= {"","Shi","Bai","Qian"};
    string bit2[]= {"Yi","Wan",""};
    int main() {
        int n;
        cin>>n;
        if(n==0) {
            cout<<"ling";
            return 0;
        }
        if(n<0) {
            cout<<"Fu ";
            n=-n;
        }
        int part[3];
        part[0]=n/100000000,part[1]=(n-n/100000000*100000000)/10000,part[2]=n%10000;
        bool zero=false;
        int printCnt=0;
        for(int i=0; i<3; i++) {
            int tmp=part[i];
            for(int j=3; j>=0; j--) {
                int curPos=8-i*4+j;
                if(curPos>=9)  continue;
                int cur=(tmp/(int)pow(10,j))%10;
                if(cur!=0) {
                    if(zero) {
                        cout<<" ling";
                        zero=false;
                    }
                    if(j==0) {
                        printCnt++==0?cout<<kv[cur]:cout<<" "<<kv[cur];
                    } else {
                        printCnt++==0?cout<<kv[cur]<<" "<<bit[j]:cout<<" "<<kv[cur]<<" "<<bit[j];
                    }
                } else {
                    if(!zero&&j!=0&&n/pow(10,curPos)>=10) zero=true;
                }
            }
            if(i!=2&&part[i]>0) cout<<" "<<bit2[i];
        }
        return 0;
    }
    

    1. https://www.liuchuo.net/archives/2204

    相关文章

      网友评论

          本文标题:1082 Read Number in Chinese (字符串

          本文链接:https://www.haomeiwen.com/subject/bmunoftx.html