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 ...
随机推荐
- Java基础语法:数组
一.简介 描述: 数组是相同类型数据的有序集合. 其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们. 基本特点: 数组的长度是确定的.数组一旦被创建,它的大小就是不可以改变的. ...
- 在Windows服务中添加定时器
创建windows服务: 在VisualStudio中用C#创建一个Windows服务,微软MSDN参考地址: http://msdn.microsoft.com/zh-cn/library/zt39 ...
- macOS网络安全审计
nettop监听网络流量的方法 nettop是macOS系统自带的命令,命令功能能监听网络流量,如果你想查询一个恶意域名.ip和本机进程连接情况,那么可以试试nettop,就是展示方式不是太友好,需要 ...
- 381. O(1) 时间插入、删除和获取随机元素 - 允许重复
381. O(1) 时间插入.删除和获取随机元素 - 允许重复 LeetCode_381 题目详情 题解分析 代码实现 package com.walegarrett.interview; impor ...
- Google单元测试框架gtest之官方sample笔记4--事件监控之内存泄漏测试
sample 10 使用event listener监控Water类的创建和销毁.在Water类中,有一个静态变量allocated,创建一次值加一,销毁一次值减一.为了实现这个功能,重载了new和d ...
- 数据库事务 ACID属性、数据库并发问题和四种隔离级别
数据库事务 ACID属性.数据库并发问题和四种隔离级别 数据库事务 数据库事务是一组逻辑操作单元,使数据从一种状态变换到另一种状态 一组逻辑操作单元:一个或多个DML操作 事务处理原则 保证所有事务都 ...
- rest framework parsers
解析器 机交互的Web服务更倾向于使用结构化的格式比发送数据格式编码的,因为他们发送比简单的形式更复杂的数据 -马尔科姆Tredinnick,Django开发组 REST框架包含许多内置的解析器类,允 ...
- CF995E Number Clicker (双向BFS)
题目链接(洛谷) 题目大意 给定两个数 \(u\) , \(v\) .有三种操作: \(u=u+1(mod\) \(p)\) . \(u=u+p−1(mod\) \(p)\) . \(u=u^{p−2 ...
- 自动化测试工具(基于WordCount作业)
本自动化测试的程序用于自动化测试WordCount作业,采用Java开发(基于jdk1.8+),基于Maven来管理项目. 支持的语言和开发进度 语言 进度 Java 已测试并投入运行 C++ 开发完 ...
- Solon 框架详解(十一)- Solon Cloud 的配置说明
Solon 详解系列文章: Solon 框架详解(一)- 快速入门 Solon 框架详解(二)- Solon的核心 Solon 框架详解(三)- Solon的web开发 Solon 框架详解(四)- ...