1082. Read Number in Chinese (25)

来源:互联网 发布:淘宝上的好评能删除吗 编辑:程序博客网 时间:2024/06/09 14:12

1082. Read Number in Chinese (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代码

参考了代码:http://blog.csdn.net/apie_czx/article/details/48270285

解题思路:

先去掉末尾的零,从第一个非零数开始加入vector字符串数组,如果当前为不是第一位并且不是零或者是万位或者亿位,需要将标识符加入数组,但万位的时候要先判断是否存在万比如600008080输出就不存在万。输出的时候要从后往前输出。连续多个零只输出一次,但在万位那里需要注意,因为可能连续零被万位隔开如600800080

#include<iostream>#include<string>#include<vector>using namespace std;string dig[] = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };string index[] = { "", "Shi", "Bai", "Qian", "Wan", "Shi", "Bai", "Qian", "Yi" };int main(){int number,n;vector<string> res;//存放结果vector<int> num;//存放每位上的数字cin>>number;n = number;if(number==0){cout<<"ling"<<endl;//number为0直接输出}else if(number<0){cout<<"Fu ";//负数要先输出负号number *= -1;}while(number != 0){num.push_back(number%10);number /= 10;}int e;for(e = 0;e < num.size()&&num[e] == 0;e++);//从低位开始找到第一位不等于0的for(int i = e;i < num.size();i++){if(i != 0&&(num[i] != 0||i == 4||i == 8)){//如果不是个位;位上的数字不是0;//是万位或亿位,则放入位标识符if(i == 4){if(n%100000000/10000 != 0){//判断是否存在万res.push_back(index[i]);}}else{res.push_back(index[i]);}}res.push_back(dig[num[i]]);}for(int i = res.size()-1;i >= 0;i--){if(i != res.size()-1){cout<<" ";}int cnt = 0;while(i >= 0&&res[i] == "ling"){//找出连续0的个数i--;cnt++;}if(cnt != 0&&res[i] != "Wan"){cout<<"ling ";}cout<<res[i];}return 0;}


原创粉丝点击