PAT1082:Read Number in Chinese
1082. Read Number in Chinese (25)
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 思路 逻辑题,有点恶心。。。
1.先输入一个数,将单个零和负数的情况先处理掉(后续将1亿的情况也单独处理掉)。
2.将该数的每一位数字用一个int数组的形式存放。
3.遍历该数组,根据该数组的每一位的位数和单个数字将对应的字符串插入到一个新的vector中,为0的位数除了是万位或者亿位以外都不用插入位数的字符串。
4.对于多余的零做处理,并输出每一位的数字和位数。 代码
#include<iostream>
#include<vector>
using namespace std;
vector<string> n={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
vector<string> digit={"","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi"}; using namespace std;
int main()
{
vector<string> res;
vector<int> number;
int num,index;
cin >> num;
if(num == 0)
{
cout << "ling" << endl;
return 0;
}
else if( num < 0)
{
cout << "Fu ";
num = -num;
}
while(num != 0)
{
number.push_back(num % 10);
num /= 10;
} for(index = 0;index < number.size() && number[index] == 0;index++);
if(index == 8)
{
cout << n[number[index]] << " Yi";
return 0;
}
for(int i = index;i < number.size();i++)
{
if(i != 0 && (number[i] != 0 || i == 4 || i == 8))
res.push_back(digit[i]);
res.push_back(n[number[i]]);
}
for(int i = res.size() - 1;i >= 0;i--)
{
if(i != res.size() - 1)
cout << " ";
int zerocnt = 0;
while( i >= 0 && res[i] == "ling")
{
i--;
zerocnt++;
}
if(zerocnt > 0 && res[i] != "Wan")
{
cout << "ling ";
}
cout << res[i];
}
}
PAT1082:Read Number in Chinese的更多相关文章
- pat1082. Read Number in Chinese (25)
1082. Read Number in Chinese (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 1082. Read Number in Chinese (25)
题目如下: Given an integer with no more than 9 digits, you are supposed to read it in the traditional Ch ...
- A1082. Read Number in Chinese
Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese ...
- PTA (Advanced Level)1082.Read Number in Chinese
Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese ...
- PAT甲级——A1082 Read Number in Chinese
Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese ...
随机推荐
- 广义线性模型 R--glm函数
R语言glm函数学习: [转载时请注明来源]:http://www.cnblogs.com/runner-ljt/ Ljt 作为一个初学者,水平有限,欢迎交流指正. glm函数介绍: glm(for ...
- OpenGL Shader Key Points (1)
1. Shader起步 1.1. 可编程管线 仅考虑Vertex shader和fragment shader: 1.2. Shader Object 在编译阶段生成,把shader源代码编译成 ...
- 数据cube的schema与sql的对应的关系
用schema workbench 设置cube的维度结构 saiku 使用的cube,会将不同维度的查询转化为sql语句. schema中, cube的事实表和dimension表进行自然连接,具体 ...
- Android 开发中遇到Read-only file system问题解决方案
问题描述: 在往scdcard中复制mp3文件时,复制不成功.查看了一下sdcard里面没有内容,且无法直接在里面创建文件会出现-- read only file system类似的内容提示. ...
- linux下ruby使用tcl/tk编程环境设置
正常情况下最新的ruby都是不带tcl/tk选项编译的,所以我们在运行tcl/tk代码时都会发生找不到tk库的错误.解决办法很简单只要以tcl/tk选项编译ruby即可. 这里以ubuntu 15.0 ...
- LeetCode之“动态规划”:Edit Distance
题目链接 题目要求: Given two words word1 and word2, find the minimum number of steps required to convert wor ...
- OpenCV问题集锦,图片显示不出来,WaitKey(0),imread()不能读图片,未经处理的异常,等问题集合
昨天根据uc伯克利的人工图像分割文件.seg,显示图像的时候调用了OpenCV的库函数,图片都能用imwrite写好,但是imshow死活显示不出来. 今天早上发现原来是imshow()后面应该加上: ...
- obj-c编程19:关联对象
对于一些无法子类化的实例对象来说,如果希望将一个对象与其绑定该如何做呢? 以下示例虚构了一个HyConsoleAlert类,User类将会使用该类在控制台显示定制的告警.如果User中包括多个Aler ...
- python snownlp情感分析简易demo
SnowNLP是国人开发的python类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的,由于现在大部分的自然语言处理库基本都是针对英文的,于是写了一个方便处理中文的类库,并且和T ...
- Read程序员的困境有感
看完这篇文章,我真的害怕了,码农遍地都是,我就是,改变从今天做起,不走码农生活 首先, 打造你自己的私人项目.你需要不断地打磨自己的技艺.如果工作本身并不能帮助你做到这一点,就捡起那些你感兴趣的问题, ...