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 ...
随机推荐
- Java 显示调用隐式调用
当你没有使用父类默认的构造方法时,此时在子类的构造方法中就需要显示的调用父类定义的构造方法.比如:父类:class Animal{ private String name; //如果你定义一个新的构造 ...
- list深拷贝和浅拷贝
在Python中,经常要对一个list进行复制.对于复制,自然的就有深拷贝与浅拷贝问题.深拷贝与浅拷贝的区别在于,当从原本的list复制出的list之后,修改其中的任意一个是否会对另一个造成影响,即这 ...
- spring@Async注解实现异步方法调用
概述 如何实现异步方法调用,很多人首先会想到使用线程或者线程池技术,springboot中有一个很简单的方法可以实现异步方法调用,那就是在方法上使用@Async注解 例子 首先在Springboot启 ...
- luogu 1373 小a和uim之大逃离 dp
有取模操作,所以直接维护模意义下的差即可. Code: #include <bits/stdc++.h> #define M 16 #define N 801 #define ll lon ...
- BZOJ 2834: 回家的路 Dijkstra
按照横,竖为方向跑一个最短路即可,算是水题~ #include <bits/stdc++.h> #define N 200005 #define E 2000000 #define set ...
- C++11正则表达式初探
C++正则表达式 在此之前都没有了解过C++的正则,不过现在大多数赛事都支持C++11了,因此有必要学习一下,用于快速A签到题. 所在头文件 #include<regex> 正则表达式语法 ...
- 微信sdk php签名方法整理
<?php class JSSDK { private $appId; private $appSecret; public function __construct($appId, $appS ...
- Java终止线程的三种方式
停止一个线程通常意味着在线程处理任务完成之前停掉正在做的操作,也就是放弃当前的操作. 在 Java 中有以下 3 种方法可以终止正在运行的线程: 使用退出标志,使线程正常退出,也就是当 run() 方 ...
- CPU分支预测器
两篇结合就ok啦 1.https://www.jianshu.com/p/be389eeba589 2.https://blog.csdn.net/edonlii/article/details/87 ...
- polya定理,环形涂色
环形涂色裸题 #include<iostream> #include<cstdio> #include<algorithm> #include<vector& ...