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 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要怎么控制输出想了很久都没有答案,看了《算法笔记上机训练实战指南》后恍然大悟,代码基本是书上的代码
- 可以借鉴的地方:
- 分段的巧妙之处,每次
r -= 4
定位到第一段随后只要r+4
就到另一段了,比我本来用的%, /
分割字符串好多了 800000008
的输出应该是ba Yi ling ba
而不是ba Yi Wan ling ba
,也就是中间的段如果全为0就不能输出多余的万
- 分段的巧妙之处,每次
代码
#include<bits/stdc++.h>
using namespace std;
char number[15][6] = {"ling", "yi", "er", "san", "si",
"wu", "liu", "qi", "ba", "jiu"};
char q[5][5] = {"Shi", "Bai", "Qian", "Wan", "Yi"};
int main()
{
string s;
cin >> s;
int l = 0, r = s.size() - 1;
if(s[0] == '-')
{
cout << "Fu";
l++;
} //负数输出"Fu",l定位到第一个位
while(l + 4 <= r) r -= 4; //数字分成a,b,c三段,先定位到最高的一段
while(l < s.size())
{
bool acum0 = false; //累加0
bool printed = false; //是否有输出过
while(l <= r)
{
if(l > 0 && s[l] == '0') acum0 = true;
else
{ //当前位不为0
if(acum0) //存在累计的0
{
cout << " ling";
acum0 = false;
}
if(l > 0) cout << " "; //非首位的话后面都要输出空格
cout << number[s[l] - '0']; //输出对应数字
printed = true; // >=1的数字被输出
if(l != r) //因为r始终在每一段的最后一位,如果不相等说明不是各位,那么就要输出对应的百十千
cout << " " << q[r - l - 1];
}
l++; //处理完当前位,进行下一位
}
if(printed && r != s.size() - 1) //非个位就输出万或亿
cout << " " << q[(s.size() - 1 - r) / 4 + 2];
r += 4;
}
return 0;
}
引用
https://pintia.cn/problem-sets/994805342720868352/problems/994805385053978624
PTA (Advanced Level)1082.Read Number in Chinese的更多相关文章
- PAT (Advanced Level) 1082. Read Number in Chinese (25)
模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PTA (Advanced Level) 1024 Palindromic Number
Palindromic Number A number that will be the same when it is written forwards or backwards is known ...
- 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 ...
- PTA(Advanced Level)1036.Boys vs Girls
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- PTA (Advanced Level) 1019 General Palindromic Number
General Palindromic Number A number that will be the same when it is written forwards or backwards i ...
- 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) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
随机推荐
- codeforces405D
Toy Sum CodeForces - 405D Little Chris is very keen on his toy blocks. His teacher, however, wants C ...
- mysql保留最新数据
直接上sql语句,亲测可用: DELETE tb FROM lotus_system_log AS tb ,(SELECT pk_id FROM lotus_system_log ORDER BY p ...
- 阿里云修改主机名hostname
一.永久修改主机名的方法(针对于普通的服务器) 1.通过hostname命令修改. [root@izwz9f7pm0tw36neb1j7gmz ~]# hostname node1 修改完之后发现主机 ...
- [CSP-S2019]:赛后总结
笔者有幸参加了$CSP-S\ 2019$,$AFO$之前,写下自己最后一篇赛后总结. $Day\ 0$ 早上起来把自己调了一晚上被卡空间的题卡过了,很开心(内存限制$256MB$,然而我的内存申请是$ ...
- Python控制台输出时刷新当前行内容而不是输出新行
需求目标 执行Python程序的时候在控制台输出内容的时候固定一刷新内容,如下: Downloading File FooFile.txt [%] 而不是 Downloading File FooFi ...
- Linux设备驱动程序 之 异步通知
尽管大多数时候阻塞型和非阻塞型操作的组合以及select方法可以有效的查询设备,但是某些时候用这种技术处理就效率不搞了: 例如:一个进程在低优先级执行长的循环计算,但又需要尽可能快的处理输入数据,如果 ...
- Linux设备驱动程序 之 ioctl
ioctl 除了读取和写入设备之外,大部分驱动程序还需要另外一种能力,即通过设备驱动程序执行各种类型的硬件控制,通常这种需求使用ioctl方法支持,该方法实现了同名的系统调用: 在用户空间,ioctl ...
- Nginx之搭建反向代理实现tomcat分布式集群
参考博文: Nginx反向代理实现Tomcat分布式集群 1. jdk 安装 jdk 下载网址: http://www.oracle.com/technetwork/java/javase/downl ...
- linux 下安装redis并用QT写客户端程序进行连接
1.安装redis.使用如下命令: wget http://dowload.redis.io/redis-stable.tar.gz tar xzf redis-stable.tar.gz cd ...
- DNA Sorting
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 105159 Accepted: 42124 De ...