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. MySql大小写配置

    新安装mysql5.7版本后,linux环境下默认是大小写敏感的.可以在客户端执行以下命令: SHOW VARIABLES LIKE '%case%' 可以看到 lower_case_table_na ...

  2. 洛谷P2789 直线交点数 [数论,递归]

    题目传送门 题目描述 平面上有N条直线,且无三线共点,那么这些直线能有多少不同的交点数? 输入格式 一个正整数N 输出格式 一个整数表示方案总数 输入输出样例 输入 #1 4 输出 #1 5 说明/提 ...

  3. Java中properties可以读取,但是里面的数据读取不到

    今天配置一个项目jdbc.properties这个文件里面的键值总是读取不到,刚开始以为是文件没有读取到,但是测试是读取到的,再排查键值是不是写错了, 后来发现键值是对的,这就很奇怪了 比较是没有任何 ...

  4. Multiism四阶巴特沃兹低通滤波器的仿真实现

    因为4阶巴特沃兹低通滤波器比较简单,所以省略设计过程和思路以及不必要的废话. 设计的滤波器的性能:截止频率大约是500HKZ,Rs = Rl = 32 欧姆. 预估滤波器大致的幅频特性曲线如下: 最初 ...

  5. codeforces#1148E. Earth Wind and Fire(贪心)

    题目链接: http://codeforces.com/contest/1148/problem/E 题意: 给出两个长度为$n$的序列,将第一个序列变成第二个序列,顺序不重要,只需要元素完全相同即可 ...

  6. LeetCode205----同构字符串

    给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一 ...

  7. js优先队列的定义和使用

    //队列,先入先出,FIFO function Queue() { this.items = []; } Queue.prototype = { constructor: Queue, enqueue ...

  8. koa 项目实战(十)使用 validator 验证表单

    1.安装模块 npm install validator -D 2.验证注册参数 根目录/validation/register.js const Validator = require('valid ...

  9. 【log4j】log4j.properties 文件示例

    # 下面的文件内容是写程序长期要用的,放在这里留个底#Output information(higher than INFO) to stdout and file.info/debug/error ...

  10. 性能测试 | 服务器CPU使用率高分析实例

    前面我们讨论系统调用的时候结论是耗时200ns-15us不等.不过我今天说的我的这个遭遇可能会让你进一步认识系统调用的真正开销.在本节里你会看到一个耗时2.5ms的connect系统调用,注意是毫秒, ...