1082. Read Number in Chinese (25)

来源:互联网 发布:linux 禅道 怎么启动 编辑:程序博客网 时间:2024/05/23 20:29
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


IDEA

1.需要把中文存到string数组中

数字:下标0~9,元素对应“ling”~"jiu"

单位:下标0~9,元素对应""~"Yi"

2.存在0的情况比较麻烦 如100800,0010等,需要特殊处理

3.程序思路:

输入整数num,是0就输入ling,程序结束;是负数,输入Fu,然后都变为正数;

将数倒序存入vector容器digit中

找到第一个不为0的数,若改数在第8位,则输出改数和Yi,程序结束

将为处理数字中将0的结果存入vector容器result中,0不存其单位

输出result中结果,并处理0的情况,多个0连续时只输出一个ling


CODE

#include<iostream>#include<cstring>#include<vector>#include<algorithm>using namespace std;int main(){string s[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};string s1[9]={"","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi"};int num;cin>>num;if(num==0){cout<<"ling";return 0;}else if(num<0){cout<<"Fu ";num=-num;}vector<int> digit;while(num){digit.push_back(num%10);num/=10;}int k=0;while(k<digit.size()&&digit[k]==0){//找到第一个不为0的数字 k++;}vector<string> result;if(k==8){cout<<s[digit[k]]<<" Yi";}else{for(int i=k;i<digit.size();i++){if(i&&(digit[i]||i==4||i==8)){result.push_back(s1[i]);}result.push_back(s[digit[i]]);}} for(int i=result.size()-1;i>=0;i--){if(i!=result.size()-1){cout<<" ";}int count=0;while(i>=0&&result[i]=="ling"){i--;count++;}if(count&&result[i]!="Wan"){cout<<"ling ";}cout<<result[i];}return 0;}


0 0
原创粉丝点击