zoukankan      html  css  js  c++  java
  • 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

    题目大意:

    给定一个不超过9位的整数,你应该用传统的中文方式阅读它~ 如果是负的,首先输出“Fu”。 例如,-123456789被读作“Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu”。 注意:零(“ling”)必须根据中国传统正确处理。 例如,100800是“yi Shi Wan ling ba Bai”~

    原文链接:https://blog.csdn.net/liuchuo/article/details/52200729

    题解

    人生感悟:看柳神题解就像看英语长难句一样困难(菜)
    这道题就属于那种费半天劲好不容易看懂,看懂也不会写,写了也记不住,记住了也会忘,忘了再看还不会系列。。。
    解读一下代码的细节(花了3轮debug才勉强知道是干嘛的Orz)

    eg1:
    curPos  8   7 6 5 4   3 2 1 0
            1 / 2 3 4 5 / 6 7 8 9
    j       0   3 2 1 0   3 2 1 0
    
    
    eg2:
    curPos  8   7 6 5 4   3 2 1 0
            0 / 0 1 0 0 / 8 0 0 0
    j       0   3 2 1 0   3 2 1 0
    
    
    eg3:
    curPos  8   7 6 5 4   3 2 1 0
            0 / 0 1 0 0 / 1 0 0 1
    j       0   3 2 1 0   3 2 1 0
    
    

    首先,柳神以一句巧妙的代码curPos = 8 - i * 4 + j;将3个部分的temp的curPos求出来,实际上他们最终的值如上所示;
    然后,对于每个temp j都是从3开始的,3-2-1-0;
    另外,对于每一当前数字的求法,则是根据temp/1000%10,temp/100%10,temp/10%10,temp/1%10求得;
    printCnt只有在刚开始为0的时候输出前面无空格的样式,后面的数字前面都有空格;
    zero的改变if (!zero && j != 0 && n / J[curPos] >= 10) zero = true;只有当temp不处于最后一个(j=0)位置时+不处于最高位时,zero->true;
    而zero的生效必须等到下一个不为0的数字出现才可以。
    目前第一遍的理解暂时就到这个深度,等以后再看再添~

    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    string num[10] = { "ling","yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };
    string c[6] = { "Ge","Shi", "Bai", "Qian", "Yi", "Wan" };
    int J[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
    vector<string> res;
    int main() {
        int n;
        freopen("1.txt", "r", stdin);
        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 % 100000000) / 10000;
        part[2] = n % 10000;
        bool zero = false; //是否在非零数字前输出合适的ling
        int printCnt = 0; //用于维护单词前没有空格,之后输入的单词都在前面加一个空格。
        for (int i = 0; i < 3; i++) {
            int temp = part[i]; //三个部分,每部分内部的命名规则都一样,都是X千X百X十X
            for (int j = 3; j >= 0; j--) {
                int curPos = 8 - i * 4 + j; //当前数字的位置
                if (curPos >= 9) continue; //最多九位数
                int cur = (temp / J[j]) % 10;//取出当前数字
                if (cur != 0) {
                    if (zero) {
                        printCnt++ == 0 ? cout<<"ling" : cout<<" ling";
                        zero = false;
                    }
                    if (j == 0)
                        printCnt++ == 0 ? cout << num[cur] : cout << ' ' << num[cur]; //在个位,直接输出
                    else
                        printCnt++ == 0 ? cout << num[cur] << ' ' << c[j] : cout << ' ' << num[cur] << ' ' << c[j]; //在其他位,还要输出十百千
                } else {
                    if (!zero && j != 0 && n / J[curPos] >= 10) zero = true;   //注意100020这样的情况
                }
            }
            if (i != 2 && part[i]>0) cout << ' ' << c[i + 4]; //处理完每部分之后,最后输出单位,Yi/Wan
        }
        return 0;
    }
    
  • 相关阅读:
    Zju1876 Edit Step Ladders
    Candy糖果盒
    哈希
    最长上升子序列 nlogn
    电费结算
    【luoguP5656】二元一次不定方程(gcd,exgcd,裴蜀定理,不定方程初步)
    DP技巧
    高精
    【CSP烤前注意】
    [Jsoi2015] 种花
  • 原文地址:https://www.cnblogs.com/moonlight1999/p/15758364.html
Copyright © 2011-2022 走看看