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 ...
随机推荐
- Eclipse集成Android_NDK
Eclipse集成Android NDK说明 2 为什么要用NDK? 以下内容引用自<Pro Android Games> Some Java develo ...
- 我所犯的JavaScript引用错误
近期在w3cschool学习JavaScript和php--学完后,开始帮一哥们友情写网站.但是在使用ajax和Jquery的时候发现,我自己写的脚本不能运行.捣鼓了半天,没有发现任何语句错误.调试器 ...
- 第一个Polymer应用 - (2)创建你自己的元素
原文链接: Step 2: Your own element翻译日期: 2014年7月6日翻译人员: 铁锚通过上一节的学习和实践, 您已经完成了一个基本的应用程序结构(application stru ...
- 内核调试神器SystemTap — 简介与使用(一)
a linux trace/probe tool. 官网:https://sourceware.org/systemtap/ 简介 SystemTap是我目前所知的最强大的内核调试工具,有些家伙甚至说 ...
- EventBus3.0 study
概述 eventbus出来很久了,最近想用一下eventbus,自己对着一些博客撸了一个demo,发现竟然crash了,然后去看看源码发现3.0的eventbus有了很多改动.技术变化真快,得保持谦虚 ...
- SSH框架项目开发命名规范
SSH 框架项目开发命名规范 一.各层包及类命名规范 总体原则:包名所有字母小写,类名采用 "驼峰标识",具体如下: 1. Action 类 包命名规范:co ...
- 恶补web之八:jQuery(2)
jquery中非常重要的部分,就是操作dom的能力: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括html标记) val() - 设置或返回表单字段 ...
- 熊猫猪新系统测试之三:iOS 8.0.2
本来本猫要等到8.1版本出来后再做测试的,结果等来等去就是迟迟不推送更新呀!说好10月20号的iOS 8.1呢?为了一鼓作气写完,就先不等了.先拿手头的iOS 8.0.2系统做一下测试吧! 8.x系统 ...
- java并发包分析之———concurrentHashMap
一.Map体系 Hashtable是JDK 5之前Map唯一线程安全的内置实现(Collections.synchronizedMap不算).Hashtable继承的是Dictionary(Hasht ...
- JMeter——简单的接口测试实例(一)
场景:使用JMeter来实现接口测试 基本流程:添加线程组->添加http信息头管理器->添加http请求->添加断言->添加监听器->执行,查看结果 案例分析:下面以办 ...