A1082. 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
- #include<iostream>
- #include<cstdio>
- #include<string.h>
- using namespace std;
- int num[] = {}, pt;
- void num2arr(int N){
- pt = ;
- do{
- num[pt++] = N % ;
- N = N / ;
- }while(N != );
- }
- char strNum[][] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
- char duan[][] = {"", "Shi", "Bai", "Qian"};
- char prt[][];
- int main(){
- int N, neg = , len = ;
- scanf("%d", &N);
- if(N < ){
- neg = -;
- N = N * -;
- }
- num2arr(N);
- if(neg == -){
- strcpy(prt[len++], "Fu");
- }
- int zeroTag = , wanTag = ;
- if(N == ){
- printf("ling");
- return ;
- }
- for(int i = pt - ; i>= ; i--){
- if(i == ){
- strcpy(prt[len++], strNum[num[i]]);
- strcpy(prt[len++], "Yi");
- }else if(num[i] == ){
- zeroTag = ;
- }else if(num[i] != ){
- if(i > && i < ){
- wanTag = ;
- }
- if(zeroTag == ){
- zeroTag = ;
- strcpy(prt[len++], strNum[]);
- }
- strcpy(prt[len++], strNum[num[i]]);
- if(i % != )
- strcpy(prt[len++], duan[i % ]);
- }
- if(i == && wanTag == ){
- strcpy(prt[len++], "Wan");
- }
- }
- for(int i = ; i < len; i++){
- if(i == len - )
- printf("%s", prt[i]);
- else printf("%s ", prt[i]);
- }
- cin >> N;
- return ;
- }
总结:
1、属于字符串处理问题,我写的不太好,写完后才发现问题,于是只能修修补补勉强过了测试点。由于题目所给的数字最多才到亿,可以分成三段(如果够分的话),亿、万、个,分别处理。对于每一段,分别读数(几千几百几十几),读完每一段再分别输出‘亿’、‘万’。
2、对于零的处理:由于多个零可能按一个零读(1001),也可能不读(1000),这个需要额外注意。可以采取这样的办法:遇到0时不要立马读出,而是设置一个zero标志,该符号表示是否已经有了未读的0,在遇到一个非0的数字时,再检查zero标志看看是否有0未读,如果有则先读0,再读这个非0数字,再修改zero标志。 另外,在读完一段数字时,需要把zero标志置为没有未读0(如 1,1230,1234)。
3、注意N = 0时的处理。注意万段全为0时不可读“万”,且要读一个0(1,0000,1234)。
4、不好直接输出的,可以先拷贝到prt数组中,之后统一输出。
A1082. Read Number in Chinese的更多相关文章
- A1082 Read Number in Chinese (25)(25 分)
A1082 Read Number in Chinese (25)(25 分) Given an integer with no more than 9 digits, you are suppose ...
- A1082 Read Number in Chinese (25 分)
1082 Read Number in Chinese (25 分) Given an integer with no more than 9 digits, you are supposed t ...
- PAT甲级——A1082 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 分)
1082 Read Number in Chinese (25 分) Given an integer with no more than 9 digits, you are supposed to ...
- PAT1082:Read Number in Chinese
1082. Read Number in Chinese (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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 ...
- pat1082. Read Number in Chinese (25)
1082. Read Number in Chinese (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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 ...
随机推荐
- 基于DDD的.NET开发框架ABP实例,多租户 (Saas)应用程序,采用.NET MVC, Angularjs, EntityFramework-介绍
介绍 基于ABPZERO的多租户 (Saas)应用程序,采用ASP.NET MVC, Angularjs-介绍 ASP.NET Boilerplate作为应用程序框架. ASP.NET MVC和ASP ...
- Object-Oriented(一)创建对象
自用备忘笔记 前言 虽然可以使用 Object 和对象字面量创建对象,但是如果要创建大量相似的对象又显得麻烦.为解决这个问题,人们开始使用工厂模式的变种. 工厂模式 function person(n ...
- webvirtmgr-重命名kvm虚拟机的名称
之前部署了Webvirtmgr平台管理kvm虚拟机,由于虚拟机在创建时名称是顺便起的,后续在虚拟机上部署了部分业务.为了便于管理,最好将虚拟机的名称重置下. 现在说下如何修改kvm中虚拟机的名称: 比 ...
- junit-test
一.题目简介: 用单元测试junit4测试calculator类的加减乘除四种方法,来初步学习junit4的学习方法. 二.源码的github链接 :https://github.com/weare ...
- js/jquery禁止页面回退
$(function() { //防止页面后退 history.pushState(null, null, document.URL); window.addEventListener('popsta ...
- 在XShell中使用sz和rz命令下载和上传文件
借助XShell,使用linux命令sz可以很方便的将服务器上的文件下载到本地,使用rz命令则是把本地文件上传到服务器 工具/原料 XShell CentOS 6.5 使用sz下载文件 1 输 ...
- PAT 1061 判断题
https://pintia.cn/problem-sets/994805260223102976/problems/994805268817231872 判断题的评判很简单,本题就要求你写个简单的程 ...
- js原生常用事件event
onblur 元素失去焦点: onchange用户改变域的内容: onclick鼠标点击对象: onerror当加载图片时发生错误: onfocus 元素获得焦点: onkeypress某个键盘的键被 ...
- hive视图
简化复杂的查询 员工好.姓名.月薪.年薪.在一个emp表中; 部门名称在dept的表中;并未年薪起了一个名字annlsal 查询视图 视图是一个虚表,是不存数据的
- Linux基础学习(5)--文本编辑器Vim
第五章——文本编辑器Vim 一. Vim常用操作 1.Vim简介: Vim是一个功能强大的全屏幕文本编辑器,是Linux/UNIX上最常用的文本编辑器,它的作用是建立.编辑. ...