题目如下:

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

题目要求按照中国人的习惯阅读一个不超过9位的数字,这道题的坑比较多,一定要考虑到所有的情况。

首先要抓住规律,我们可以发现,一个数字的读法在每个4位是一致的。

例如12341234,我们读作“一千二百三十四万一千二百三十四”,我们可以看到除去万字以外读法完全一致。因此我们集中精力解决四位数的读法,然后加上亿、万等即可。

在解决的时候,注意0的读法,例如1000读作一千,而1050读作一千零五十,1005读作一千零五,另外根据题目要求,10读作一十而不是十。

四位的读法分析:一次性传入4位,高位允许全0,因为要处理不同部位的4位。

①一位数直接读,读作零到九。

②两位数判断十位是否是0,如果是,并且个位不是0,设个位为x,应该读作零x,例如10,0005,传入0005,整个数应该读作十万零五;如果十位为0并且个位为0,则不读。例如10,0000,万已经由高4位处理完毕,读作10万,低位不必读。

③三位数、四位数和两位数的思路一致,首先判断是否为0,如果发现当前位为0并且该位后面有不为0的,应该读一个零,注意不要读多了。

④一般情况只需要按照不同的位先输出数字,然后输出Qian Bai Shi即可,注意缩进处理。

把处理四位数的方法封装成一个函数。

整个数的处理方法为≤4位的直接用上面的函数,>4的截取不同的字符段来得到不同的位,每4位一截取。

代码如下:

#include <iostream>
#include <string>
#include <string.h>
#include <sstream>
#include <stdio.h> using namespace std; char* values[] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; int char2int(char c){
return c - '0';
} void handleNum(string num){ int bits = num.length(); bool printZero = false;
switch(bits){
case 1:{
int ge = char2int(num[0]);
printf("%s",values[ge]);
break;
}
case 2:{
int shi = char2int(num[0]);
int ge = char2int(num[1]);
if(shi != 0) printf("%s Shi",values[shi]);
else if(!printZero){
printf("ling");
printZero = true;
}
if(ge != 0) printf(" %s",values[ge]);
break;
}
case 3:{
int bai = char2int(num[0]);
int shi = char2int(num[1]);
int ge = char2int(num[2]);
if(bai != 0) {printf("%s Bai",values[bai]); printZero = false;}
else if(!printZero && (shi !=0 || ge != 0)){
printf("ling");
printZero = true;
}
if(shi != 0) { printf(" %s Shi",values[shi]); printZero = false; }
else if(!printZero && ge!=0){
printf(" ling");
printZero = true;
}
if(ge != 0) printf(" %s",values[ge]);
break;
}
case 4:{
int qian = char2int(num[0]);
int bai = char2int(num[1]);
int shi = char2int(num[2]);
int ge = char2int(num[3]);
if(qian != 0) {printf("%s Qian",values[qian]); printZero = false;}
else if(!printZero && (bai!=0 || shi!=0 || ge!=0)){
printf("ling");
printZero = true;
}
if(bai != 0) {printf(" %s Bai",values[bai]); printZero = false;}
else if(!printZero && (shi != 0 || ge != 0)){
printf(" ling");
printZero = true;
}
if(shi != 0) {printf(" %s Shi",values[shi]); printZero = false;}
else if(!printZero && ge != 0){
printf(" ling");
printZero = true;
}
if(ge != 0) printf(" %s",values[ge]);
}
} } int main()
{
string num;
cin >> num;
if(num[0] == '-'){
num = num.substr(1);
cout << "Fu ";
}
int bits = num.length();
switch(bits){
case 1:
case 2:
case 3:
case 4:
handleNum(num);
break;
case 5:{
int wan = char2int(num[0]);
printf("%s Wan ",values[wan]);
handleNum(num.substr(1));
break;
}
case 6:{
handleNum(num.substr(0,2));
printf(" Wan ");
handleNum(num.substr(2));
break;
}
case 7:{
handleNum(num.substr(0,3));
printf(" Wan ");
handleNum(num.substr(3));
break;
}
case 8:{
handleNum(num.substr(0,4));
printf(" Wan ");
handleNum(num.substr(4));
break;
}
case 9:{
handleNum(num.substr(0,1));
printf(" Yi ");
handleNum(num.substr(1,4));
printf(" Wan ");
handleNum(num.substr(5));
break;
}
}
return 0;
}

1082. Read Number in Chinese (25)的更多相关文章

  1. 1082. Read Number in Chinese (25)-字符串处理

    题意就是给出9位以内的数字,按照汉子的读法读出来. 读法请看下方的几个例子: 5 0505 0505 伍亿零伍佰零伍万零伍佰零伍 5 5050 5050 伍亿伍仟零伍拾万伍仟零伍拾  (原本我以为这个 ...

  2. 1082 Read Number in Chinese (25分)

    // 1082.cpp : 定义控制台应用程序的入口点. // #include <iostream> #include <string> #include <vecto ...

  3. PAT (Advanced Level) 1082. Read Number in Chinese (25)

    模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  4. 【PAT甲级】1082 Read Number in Chinese (25 分)

    题意: 输入一个九位整数,输出它的汉字读法(用拼音表示). trick: 字符串数组""其实会输出一个空格,而不是什么都不输出,导致测试点0和4格式错误. AAAAAccepted ...

  5. pat1082. Read Number in Chinese (25)

    1082. Read Number in Chinese (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  6. A1082 Read Number in Chinese (25 分)

    1082 Read Number in Chinese (25 分)   Given an integer with no more than 9 digits, you are supposed t ...

  7. 1082 Read Number in Chinese (25 分)

    1082 Read Number in Chinese (25 分) Given an integer with no more than 9 digits, you are supposed to ...

  8. PAT 1082 Read Number in Chinese[难]

    1082 Read Number in Chinese (25 分) Given an integer with no more than 9 digits, you are supposed to ...

  9. A1082 Read Number in Chinese (25)(25 分)

    A1082 Read Number in Chinese (25)(25 分) Given an integer with no more than 9 digits, you are suppose ...

随机推荐

  1. Go实现海量日志收集系统(三)

    再次整理了一下这个日志收集系统的框,如下图 这次要实现的代码的整体逻辑为: 完整代码地址为: https://github.com/pythonsite/logagent etcd介绍 高可用的分布式 ...

  2. python day3_liaoxuefeng

    1.Python的循环有两种,一种是for...in循环,依次把list或tuple中的每个元素迭代出来,看例子: names = ['Michael', 'Bob', 'Tracy'] for na ...

  3. PHP+JQuery+Ajax初始化网站基本信息(附源码)--PHP

    一.思路 为了保存用户会员信息的时间长一些,不局限于session的关闭.我们需要将用户信息保存在数据库中,前台每次登录都需要进行校验,来查看用看用户会员信息是否过期,如果没有过期,取出用户会员信息存 ...

  4. VMWare 学习目录

    Linux介绍 Linux入门--个人感想 Google怎么用linux 初入Linux Windows XP硬盘安装Ubuntu 12.04双系统图文详解 实例讲解虚拟机3种网络模式(桥接.nat. ...

  5. div英文内容超过div长度

    添加css word-break: normal;word-wrap: break-word;

  6. 将一个div置于另一个div之上

    div piao置于div bg之上 <div class="bg"> <div class="piao" style="backg ...

  7. 模仿天猫实战【SSM版】——后台开发

    上一篇文章链接:模仿天猫实战[SSM版]--项目起步 后台需求分析 在开始码代码之前,还是需要先清楚自己要做什么事情,后台具体需要实现哪些功能: 注意: 订单.用户.订单.推荐链接均不提供增删的功能. ...

  8. 关于 minor allele frequency(次等位基因频率)的理解

    引用自NCBI的概念(https://www.ncbi.nlm.nih.gov/projects/SNP/docs/rs_attributes.html#gmaf) Global minor alle ...

  9. How To determine DDIC Check Table, Domain and Get Table Field Text Data For Value?

     How To determineDDIC Check Table, Domain and Get Table Field Text Data For Value? 1.Get Table Fie ...

  10. Spring常用配置

    ----------------------------------------------------------------------------------------------[版权申明: ...