1082 Read Number in Chinese
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
题意:
给出一个数字,然后输出汉语读法。
思路:
模拟。(注意特例0的输出)
Code:
1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 int main() {
6 string str;
7 cin >> str;
8 if (stoi(str) == 0) {
9 cout << "ling" << endl;
10 return 0;
11 }
12 stack<string> ans;
13 bool isNegative = false;
14 if (str[0] == '-') {
15 str = str.substr(1);
16 isNegative = true;
17 }
18 int index = str.length() - 1;
19 string dummy[4] = {"+", "Shi", "Bai", "Qian"};
20 string numInCn[10] = {"ling", "yi", "er", "san", "si",
21 "wu", "liu", "qi", "ba", "jiu"};
22 while (index >= 0) {
23 bool notZero = false;
24 for (int i = 0; i < 4 && index >= 0; ++i, --index) {
25 if (str[index] == '0' && !notZero)
26 continue;
27 else {
28 if (str[index] == '0') {
29 ans.push(numInCn[str[index] - '0']);
30 } else {
31 if (i != 0) ans.push(dummy[i]);
32 ans.push(numInCn[str[index] - '0']);
33 }
34 notZero = true;
35 }
36 }
37 notZero = false;
38 if (index >= 0) ans.push("Wan");
39 for (int i = 0; i < 4 && index >= 0; ++i, --index) {
40 if (str[index] == '0' && !notZero)
41 continue;
42 else {
43 if (str[index] == '0') {
44 ans.push(numInCn[str[index] - '0']);
45 } else {
46 if (i != 0) ans.push(dummy[i]);
47 ans.push(numInCn[str[index] - '0']);
48 }
49 notZero = true;
50 }
51 }
52 if (index >= 0) {
53 ans.push("Yi");
54 ans.push(numInCn[str[index] - '0']);
55 --index;
56 }
57 }
58 if (isNegative) {
59 cout << "Fu";
60 while (!ans.empty()) {
61 cout << " " << ans.top();
62 ans.pop();
63 }
64 } else {
65 cout << ans.top();
66 ans.pop();
67 while (!ans.empty()) {
68 cout << " " << ans.top();
69 ans.pop();
70 }
71 }
72
73 return 0;
74 }
1082 Read Number in Chinese的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 1082. Read Number in Chinese (25)-字符串处理
题意就是给出9位以内的数字,按照汉子的读法读出来. 读法请看下方的几个例子: 5 0505 0505 伍亿零伍佰零伍万零伍佰零伍 5 5050 5050 伍亿伍仟零伍拾万伍仟零伍拾 (原本我以为这个 ...
- 1082 Read Number in Chinese (25分)
// 1082.cpp : 定义控制台应用程序的入口点. // #include <iostream> #include <string> #include <vecto ...
- PAT (Advanced Level) 1082. Read Number in Chinese (25)
模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT 1082. Read Number in Chinese
#include <cstdio> #include <cstdlib> #include <string> #include <vector> #in ...
- 【PAT甲级】1082 Read Number in Chinese (25 分)
题意: 输入一个九位整数,输出它的汉字读法(用拼音表示). trick: 字符串数组""其实会输出一个空格,而不是什么都不输出,导致测试点0和4格式错误. AAAAAccepted ...
随机推荐
- 第48天学习打卡(CSS)
HTML + CSS +JavaScript 结构+表现+交互 HTML:结构 CSS:表现 JavaScript:交互 1什么是CSS 如何学习 1.CSS是什么 2.CSS怎么用(快速入门 ...
- ImportError: No module named _ssl解决方法
import ssl时出现ImportError: No module named _ssl错误是因为咱安装Python的时候没有把ssl模块编译进去导致的. 解决步骤: 系统没有openssl,手动 ...
- 重复代码的克星,高效工具 VSCode snippets 的使用指南
为什么要用 snippets(代码段)? 不管你使用何种编程语言,在我们日常的编码工作中,都会存在有大量的重复代码编写,例如: 日志打印: console.log,log.info('...') 输出 ...
- 在linux下如何搭建jmeter的环境
首先 我们可以选择不同版本的jmeter 转载原连接:https://blog.csdn.net/lyl0724/article/details/79474388 Jmeter历史版本下载地址 htt ...
- 2019HDU多校第七场 HDU6656 Kejin Player H 【期望递归】
一.题目 Kejin Player H 二.分析 因为在当前等级$i$,如果升级失败可能会退回到原来的某一等级$x$,相当于就是失败的期望就是$E + (Sum[i-1] - Sum[x-1]) + ...
- JAVA-常用集合类型转换例子
package com.net.xinfang.reflect; import java.util.ArrayList; import java.util.Arrays; import java.ut ...
- css字体的属性
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...
- python创建一个二维列表
方法一:利用for-in语句来生成一个二维列表 a = [] 2 for i in range(10): 3 a.append([]) 4 for j in range(10): 5 a[i].app ...
- Envoy 部署类型
目录 Envoy 网络拓扑及请求流程 1. 术语 2. 网络拓扑 3. 配置 4. 更高层的架构 5. 请求流程 1. Listener TCP 接收 2. 侦听器过滤器链和网络过滤器链匹配 3.TL ...
- 你说,怎么把Bean塞到Spring容器?
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 小傅哥,你是怎么学习的? 有很多初学编程或者码了几年CRUD砖的小伙伴问我,该怎么学 ...