1082. Read Number in Chinese (25)

来源:互联网 发布:淘宝远程装系统骗局 编辑:程序博客网 时间:2024/05/21 00:49

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
这个题用对方法很简单,第一次做的时候是从高位向低位处理,这样很麻烦。因为输出还是不输出中间的ling由更低位决定,再没有处理到低位之前并不知道是否要输出ling。
但是从低位向高位处理时并不需要知道高位如何便可确定读法,所以可以顺着处理下来。这里放上两种代码。
第一次做:
#include <iostream>#include <vector>#include <string>#include <deque>#include <vector>using namespace std;char * num[10] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };char * pos[] = { "","","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi" };inline int char2int(char c) { return c - '0'; }//108000608:yi yi ling ba bai wan ling liu bai ling ba //100000001int main(){string raw_input;string s;vector<string> res;cin >> raw_input;if (raw_input[0] == '-')res.push_back("Fu");for (int i = 0; i < raw_input.size(); i++){if (raw_input[i] != '0' && raw_input[i] != '-'){s = raw_input.substr(i);break;}}if (s.size() == 0){cout << "ling";return 0;}int p = s.size();int i = 0;int chop = p;bool tail = false;if (p == 9){res.push_back(num[char2int(s[i])]); res.push_back(pos[p]);i++; p--; chop--;}chop = 0;for (; p > 4; p--, i++){if (s[i] == '0')chop = p;if (s[i] != '0' && p != 5){if (p < chop){res.push_back("ling");}chop = 0;res.push_back(num[char2int(s[i])]); res.push_back(pos[p]);tail = true;}if (p == 5){if (p < chop)res.push_back("ling");if (s[i] == '0' && tail)res.push_back("Wan");else if (s[i] != '0') { res.push_back(num[char2int(s[i])]); res.push_back(pos[p]); }}}chop = 0; tail = false;for (; p > 0; p--, i++){if (s[i] == '0')chop = p;if (s[i] != '0' && p != 1){if (p < chop){res.push_back("ling");}chop = 0;tail = true;res.push_back(num[char2int(s[i])]); res.push_back(pos[p]);}if (p == 1){if (p < chop)res.push_back("ling");if (s[i] == '0' && tail);else if (s[i] != '0')res.push_back(num[char2int(s[i])]);}}for (int i = 0; i < res.size(); i++){if (i != res.size() - 1)cout << res[i] << " ";else cout << res[i];}system("pause");return 0;}

第二次做:
#include <iostream>#include <string>#include <deque>using namespace std;char * num[10] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };char * pos[] = { "","","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi" };inline int char2int(char c) { return c - '0'; }int main(){string number;cin>>number;bool sign=false;if(number[0] == '-'){sign=true;number.erase(0,1);}for(int i=0;i<number.size();i++)if(number[i] != '0'){number=number.substr(i);break;}deque<string> res;if(number[0] == '0')res.push_front("ling");bool zero;int count=number.size();for(int p = 1;p <=count;p++){char cur=number[count-p];if(p % 4 == 1){zero = false;}if(cur != '0'){if(p != 1)res.push_front(pos[p]);res.push_front(num[char2int(cur)]);zero=true;}else {if(zero == true)res.push_front("ling");if(p % 4 == 1 && p != 1)res.push_front(pos[p]);}}if(sign == true)res.push_front("Fu");for(int i=0;i<res.size();i++){if(i != res.size() -1)cout<<res[i]<<" ";elsecout<<res[i]<<endl;}return 0;}


第三写写,一次比一次思路清晰,代码简短
#include <iostream>#include <string>#include <deque>using namespace std;char digital[][10] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};char name[][10]={"","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi"};int main(){string s;cin>>s;deque<string> res;if(s[0] == '0'){cout<<"ling";return 0;}for(int i=0;i<s.size();i++){int idx = s.size() - i -1;if(s[idx] == '-'){res.push_front("Fu");break;}if(s[idx] != '0'){res.push_front(name[i]);res.push_front(digital[s[idx] - '0']);}else{if(i%4 == 0)res.push_front(name[i]);if(res.front() != "ling" && res.front() != "" && res.front() != "Wan"){res.push_front("ling");}}}bool first = true;while(res.empty() == false){if(res.front() != ""){if(first == true){cout<<res.front();first = false;}else cout<<" "<<res.front();}res.pop_front();}cout<<endl;system("pause");return 0;}


0 0