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 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要正确处理。
//我一看见这个题就蒙了,完全没有思路。
代码来自:https://www.liuchuo.net/archives/2204
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string num[] = { "ling","yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };
string c[] = { "Ge","Shi", "Bai", "Qian", "Yi", "Wan" };
int J[] = {, , , , , , , , };
vector<string> res;
int main() {
int n;
cin >> n;
if (n == ) {
cout << "ling";
return ;
}
if (n < ) {
cout << "Fu ";
n = -n;
}
int part[];
part[]= n / ;//亿以上的
part[]= (n % ) / ;//千万级
part[] = n % ;//万级
bool zero = false; //是否在非零数字前输出合适的ling
int printCnt = ; //用于维护单词前没有空格,之后输入的单词都在前面加一个空格。
for (int i = ; i < ; i++) {
int temp = part[i]; //三个部分,每部分内部的命名规则都一样,都是X千X百X十X
for (int j = ; j >= ; j--) {
int curPos = - i * + j; //当前数字的位置
if (curPos >= ) continue; //最多九位数
int cur = (temp / J[j]) % ;//取出当前数字
if (cur != ) {
if (zero) {
printCnt++ == ? cout<<"ling" : cout<<" ling";
zero = false;
}
if (j == )
printCnt++ == ? cout << num[cur] : cout << ' ' << num[cur]; //在个位,直接输出
else
printCnt++ == ? cout << num[cur] << ' ' << c[j] : cout << ' ' << num[cur] << ' ' << c[j]; //在其他位,还要输出十百千
} else {
if (!zero&&j != && n / J[curPos] >= ) zero = true; //注意100020这样的情况
}
}
if (i != && part[i]>) cout << ' ' << c[i + ]; //处理完每部分之后,最后输出单位,Yi/Wan
}
return ;
}
//这个逻辑太难了。我是写不出来的,勉强看懂。
PAT 1082 Read Number in Chinese[难]的更多相关文章
- PAT 1082. Read Number in Chinese
#include <cstdio> #include <cstdlib> #include <string> #include <vector> #in ...
- 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 (Advanced Level) 1082. Read Number in Chinese (25)
模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- 【PAT甲级】1082 Read Number in Chinese (25 分)
题意: 输入一个九位整数,输出它的汉字读法(用拼音表示). trick: 字符串数组""其实会输出一个空格,而不是什么都不输出,导致测试点0和4格式错误. AAAAAccepted ...
- 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)1082.Read Number in Chinese
Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese ...
- 1082 Read Number in Chinese
Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese ...
- 1082. Read Number in Chinese (25)-字符串处理
题意就是给出9位以内的数字,按照汉子的读法读出来. 读法请看下方的几个例子: 5 0505 0505 伍亿零伍佰零伍万零伍佰零伍 5 5050 5050 伍亿伍仟零伍拾万伍仟零伍拾 (原本我以为这个 ...
- 1082 Read Number in Chinese (25分)
// 1082.cpp : 定义控制台应用程序的入口点. // #include <iostream> #include <string> #include <vecto ...
随机推荐
- Environment.GetEnvironmentVariable
参考: https://jingyan.baidu.com/article/b24f6c82cba6dc86bfe5da9f.html https://msdn.microsoft.com/zh-cn ...
- 关于BroadCastReceiver安全性的思考
尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38948863 BroadCastReceiver是Android 四大组件之中的一个,应用非 ...
- 面向对象----构造方法、this 关键字、函数的参数传递、package语句/import语句
构造方法 构造器的定义.作用 构造方法的特征 它具有与类相同的名称:它不含返回值: 注意:在构造方法里不含返回值的概念是不同于“void”的,在定义构造方法时加了“void”,结果这个方法就不再被自动 ...
- laravel 控制器多个方法共用一个路由
直接上代码: Route::get('Index/{action}', function(App\Http\Controllers\IndexController $index, $action){ ...
- Android 5.0 API新增和改进
开始开发 要构建 Android 5.0 版应用,您必须先下载 Android SDK,然后使用 SDK 管理器下载 Android 5.0 SDK 平台和系统映像. 更新您的目标 API 级别 要进 ...
- Android 使用ListView显示信息列表
课程目标1.理解ListView的基础使用2.学会熟练运用两种适配器(ArrayAdapter.SimpleAdapter)3.学会熟练运用两种监听器(OnScrollListener.OnItemC ...
- iOS - UITableView判断reloadData加载数据已经结束
问题: stackoverflow上有人提问这样的问题 http://stackoverflow.com/questions/16071503/how-to-tell-when-uitableview ...
- 《转》python学习(8)元组
转自 http://www.cnblogs.com/BeginMan/p/3156235.html 一.元组特性 1.类似列表,但不可变类型,正因如此,它可以做一个字典的key2.当处理一组对象时,这 ...
- Servlet MVC 项目实战实例
MVC的架构模式,一直是JavaEE开发中所遵循的标准,如今很多框架都已经很好的实现了MVC,像大家所熟知的Struts,SpringMVC,JSF等,但是如果没有任何框架的支持,仅仅通过JavaWe ...
- bond的操作方式
本文转自网上,版权归属原作者,原文地址 :https://www.cnblogs.com/5201351/p/4898342.html 操作系统:CentOS Linux release 7.1.15 ...