1. 程式人生 > >Read Number in Chinese (25)

Read Number in Chinese (25)

using opened ron str contain alt The lose nbsp

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".

輸入描述:

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



輸出描述:

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.

輸入例子:

-123456789

輸出例子:

Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
將阿拉伯數字轉換為漢語拼音的數字讀法,數字最大到億位。做這道題之前必須要把各種讀數的情況給分清楚
技術分享圖片
#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}; int main() { freopen("in.txt","r",stdin); 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 % 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這樣的情況,0最小也要從10位開始才讀,10位之前如果有0要讀出來,肯定n/j[curpos]>=10 } } if (i != 2 && part[i]>0) cout << << c[i + 4]; //處理完每部分之後,最後輸出單位,Yi/Wan } return 0; }
View Code

Read Number in Chinese (25)