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 ...
随机推荐
- P1129 [ZJOI2007]矩阵游戏 二分图匹配
思路:脑子+二分图匹配 提交:1次(课上讲过) 题解: 发现:如果符合题意,那么行和列一定是一一匹配的(必要条件),所以最大匹配必须是$n$. 同时我们发现,一定可以通过交换行列的方式,将(看起来)有 ...
- 【线性代数】4-3:最小二乘近似(Least Squares Approximations)
title: [线性代数]4-3:最小二乘近似(Least Squares Approximations) categories: Mathematic Linear Algebra keywords ...
- mysql查询字段中含有中文
查询mysql数据库中字段中含有中文使用正则表达式: 例如: select create_time,nickname from eb_engineer where not(nickname regex ...
- elastic search&logstash&kibana 学习历程(四)kibana安装部署和使用
kibana在linux上的部署安装 运行环境是centos7 基于jdk8 下载安装包:wget https://artifacts.elastic.co/downloads/kibana/kiba ...
- LeetCode 简化路径(探索字节跳动)
题目描述 给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如, path = "/home/", => "/home" path ...
- 阿里云AHAS应用高可用服务初体验
AHAS是阿里云提供的应用高可用服务(Application High Availability Service)产品. 高可用这个关键词可以说是互联网及软件开发行业热度一直很高的词语了,阿里云推出的 ...
- 【互联网运营P1】
一.导论 [运营]是什么 二.运营的职业分工和职能发展 三.转化型文案 4个高转化率短文案的常见姿势 2个短文案写作的核心要则 中长型转化文案的写作 针对所有问题点依次进行详细解读 四.第三方推广 常 ...
- 中间件 | kafka简介、使用场景、设计原理、主要配置及集群搭建
开源Java学习 公众号 一.入门 1.简介 Kafka is a distributed,partitioned,replicated commit logservice.它提供了类似于JMS的特性 ...
- Hander创建消息
每一个消息都需要被指定的Handler处理,通过Handler创建消息便可以完成此功能.Android消息机制中引入了消息池.Handler创建消息时首先查询消息池中是否有消息存在,如果有直接从消息池 ...
- C++中重载、重定义、重写概念辨析
重载:函数名相同,函数的参数个数.参数类型或参数顺序三者中必须至少有一种不同.函数返回值的类型可以相同,也可以不相同.发生在一个类内部. 重定义:也叫做隐藏.覆盖,子类重新定义父类中有相同名称的非虚函 ...