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的更多相关文章

  1. PAT (Advanced Level) 1082. Read Number in Chinese (25)

    模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  2. PTA (Advanced Level) 1024 Palindromic Number

    Palindromic Number A number that will be the same when it is written forwards or backwards is known ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  9. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

随机推荐

  1. Docker Gitlib创建项目后仓库连接IP地址不一致问题(包括进入docker中容器命令及退出命令)

    首次在内网搭建Gitlab环境,在成功后在Gitlab上新建了一个项目. 然而在IDEA上clone项目时发现,项目地址如下: git@0096ce63c43f:root/jump.git 或者这样 ...

  2. BZOJ3514 Codechef MARCH14 GERALD07加强版 LCT+可持久化线段树

    自己独自想出来并切掉还是很开心的~ Code: #include <bits/stdc++.h> #define N 400005 #define inf 1000000000 #defi ...

  3. 将.py脚本打包成.exe

    https://www.cnblogs.com/wyl-0120/p/10823102.html 为了方便使用,通过pyinstaller对脚本进行打包成exe文件. pip3 install pyi ...

  4. HDU 5863 cjj's string game ( 16年多校10 G 题、矩阵快速幂优化线性递推DP )

    题目链接 题意 : 有种不同的字符,每种字符有无限个,要求用这k种字符构造两个长度为n的字符串a和b,使得a串和b串的最长公共部分长度恰为m,问方案数 分析 : 直觉是DP 不过当时看到 n 很大.但 ...

  5. jQuery的$符号

    jQuery使用$的原因是: 书写简洁, 相对于其他字符与众不同, 容易被记住. JQuery占用了两个变量: $ 和 jQuery. 当在代码中打印 $ 和 jQuery时: <script ...

  6. JavaWeb_(Hibernate框架)Hibernate论坛项目中多对多案例

    基于SSH论坛小型项目 传送门 用户和发帖进行举例 多对多关系:多个用户可以回复多个帖子 因此引入了一张回复表,用来保存用户id和帖子id CREATE TABLE `hforum`.`answer` ...

  7. mybatis-puls 字段为null时候的更新问题

    在mybatis-puls重设置的全局更新策略 为null的字段忽略更新.但是在某些业务需求下面,可能需要某些字段更新为null值.那么改如何设置 1, 在你的实体属性上面单独添加需要更新nu l l ...

  8. C#_选择结构,Console的应用,数据类型转换

    1:先看一个顺序结构编程,代码如下: using System; using System.Collections.Generic; using System.Linq; using System.T ...

  9. IO之复制文件的四种方式

    1. 使用FileStreams复制 这是最经典的方式将一个文件的内容复制到另一个文件中. 使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B. 这是 ...

  10. java按某个字段对数据分组

    import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; i ...