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 ...
随机推荐
- 死磕Spring之IoC篇 - 开启 Bean 的加载
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
- 剑指 Offer 22. 链表中倒数第k个节点
剑指 Offer 22. 链表中倒数第k个节点 Offer 22 常规解法 常规解法其实很容易可以想到,只需要先求出链表的长度,然后再次遍历取指定长度的链接即可. package com.walega ...
- TKE 容器网络中的 ARP Overflow 问题探究及其解决之道
作者朱瑜坚,腾讯云后台开发工程师,熟悉 CNI 容器网络相关技术,负责腾讯云 TKE 的容器网络的构建和相关网络组件的开发维护工作,作为主力开发实现了 TKE 下一代容器网络方案. 1. 问题背景 1 ...
- Java 中为什么要设计包装类
尽人事,听天命.博主东南大学硕士在读,热爱健身和篮球,乐于分享技术相关的所见所得,关注公众号 @ 飞天小牛肉,第一时间获取文章更新,成长的路上我们一起进步 本文已收录于 「CS-Wiki」Gitee ...
- const修饰符相关
const修饰符相关 const修饰符表明一个变量是常量,大致分为三类:常量数组(等同于常量指针),常量指针,指向常量的指针. 常量数组中数据都是不可修改的,任何试图修改常量数组中的数据的操作都会报错 ...
- Linux速通08 网络原理及基础设置、软件包管理
使用 ifconfig命令来维护网络 # ifconfig 命令:显示所有正在启动的网卡的详细信息或设定系统中网卡的 IP地址 # 应用 ifconfig命令设定网卡的 IP地址: * 例:修改 et ...
- LNMP配置——Nginx配置 —— Nginx解析PHP
一.配置 #vi /usr/local/nginx/conf/vhost/test.com.conf 写入: server { listen 80; server_name test.com test ...
- 获取SpringBoot中所有的url和其参数
获取所有url和方法的对应关系 1 @Data 2 public class Param { 3 4 /** 5 * 字段名称 6 */ 7 private String name; 8 9 /** ...
- slickgrid ( nsunleo-slickgrid ) 5 增加子件
slickgrid ( nsunleo-slickgrid ) 5 增加子件 上次把单元格切换的问题解决了,这次要最做的事情就是给slickgrid的treegird增加子件,我们先选中某一条记录,然 ...
- 关于Python编写时候的一些数据格式调用问题
utf-8 可变长度字符串,互联网通用,目的是减少内存占用Unicode 万国码, 对于英文多占用一个字节ASCII码 美国编码1个字节Gb2313 中国编码 编码 encode解码 decodepy ...