zoukankan      html  css  js  c++  java
  • 1082 Read Number in Chinese

    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

    题意:

      给出一个数字,然后输出汉语读法。

    思路:

      模拟。(注意特例0的输出)

    Code:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int main() {
     6     string str;
     7     cin >> str;
     8     if (stoi(str) == 0) {
     9         cout << "ling" << endl;
    10         return 0;
    11     }
    12     stack<string> ans;
    13     bool isNegative = false;
    14     if (str[0] == '-') {
    15         str = str.substr(1);
    16         isNegative = true;
    17     }
    18     int index = str.length() - 1;
    19     string dummy[4] = {"+", "Shi", "Bai", "Qian"};
    20     string numInCn[10] = {"ling", "yi",  "er", "san", "si",
    21                           "wu",   "liu", "qi", "ba",  "jiu"};
    22     while (index >= 0) {
    23         bool notZero = false;
    24         for (int i = 0; i < 4 && index >= 0; ++i, --index) {
    25             if (str[index] == '0' && !notZero)
    26                 continue;
    27             else {
    28                 if (str[index] == '0') {
    29                     ans.push(numInCn[str[index] - '0']);
    30                 } else {
    31                     if (i != 0) ans.push(dummy[i]);
    32                     ans.push(numInCn[str[index] - '0']);
    33                 }
    34                 notZero = true;
    35             }
    36         }
    37         notZero = false;
    38         if (index >= 0) ans.push("Wan");
    39         for (int i = 0; i < 4 && index >= 0; ++i, --index) {
    40             if (str[index] == '0' && !notZero)
    41                 continue;
    42             else {
    43                 if (str[index] == '0') {
    44                     ans.push(numInCn[str[index] - '0']);
    45                 } else {
    46                     if (i != 0) ans.push(dummy[i]);
    47                     ans.push(numInCn[str[index] - '0']);
    48                 }
    49                 notZero = true;
    50             }
    51         }
    52         if (index >= 0) {
    53             ans.push("Yi");
    54             ans.push(numInCn[str[index] - '0']);
    55             --index;
    56         }
    57     }
    58     if (isNegative) {
    59         cout << "Fu";
    60         while (!ans.empty()) {
    61             cout << " " << ans.top();
    62             ans.pop();
    63         }
    64     } else {
    65         cout << ans.top();
    66         ans.pop();
    67         while (!ans.empty()) {
    68             cout << " " << ans.top();
    69             ans.pop();
    70         }
    71     }
    72 
    73     return 0;
    74 }
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    Oracle不同版本中序列的注意点
    SQLite – LIMIT子句
    LeetCode:219. Contains Duplicate II
    python返回值进行unpack
    Android编程权威指南第三版 第32章
    ThinkPHP使用soapclient调用webservice接口
    C++杂记
    关于Docker清理
    Leetcode 063 不同路径二
    第五章:详解广播机制
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12802460.html
Copyright © 2011-2022 走看看