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 ...
随机推荐
- Python_装饰器_29
# 装饰器形成的过程 : 最简单的装饰器 有返回值的 有一个参数 万能参数 # 装饰器的作用 # 原则 :开放封闭原则 # 语法糖 :@ # 装饰器的固定模式 import time # print( ...
- C语言与汇编衔接1
研究实验二 问题研究过程: 发问:C语言中的变量究竟是什么,通过下面的程序进行C语言中的变量的学习 图1 URL.EXE函数 为了研究main函数的首地址,我首先自作聪明的用了一条_DX=main, ...
- 雅思听听app
最近本人呢,正在紧张的备战雅思考试,因为英语基础很弱,尤其是听力,所以老师推荐了雅思听听这个app,说是特别好使,用了一个多月的,总体来说感觉还是很nice的,但是还有一些小毛病,不过这小毛病瑕不掩瑜 ...
- 《Linux内核分析》第八周学习小结 进程的切换和系统的一般执行过程
进程的切换和系统的一般执行过程 一.进程调度的三个时机: 1.中断处理过程(包括时钟中断.I/O中断.系统调用和异常)中,直接调用schedule(),或者返回用户态时根据need_resched标记 ...
- 2017-2018-2 1723《程序设计与数据结构》实验四 & 实验五 & 课程总结 总结
作业地址 实验四作业:https://edu.cnblogs.com/campus/besti/CS-IMIS-1723/homework/1943 提交情况如图: 实验五作业:https://edu ...
- junit-test
一.题目简介: 用单元测试junit4测试calculator类的加减乘除四种方法,来初步学习junit4的学习方法. 二.源码的github链接 :https://github.com/weare ...
- 递归拼装Tree结构数据
@Override public List<Map<String, Object>> queryListTree() { List<Map<String,Objec ...
- CRM 数据查重
2.8 小工具 · 纷享销客产品手册https://www.fxiaoke.com/mob/guide/crmdoc/src/2-8%E5%B0%8F%E5%B7%A5%E5%85%B7.html C ...
- js原生常用事件event
onblur 元素失去焦点: onchange用户改变域的内容: onclick鼠标点击对象: onerror当加载图片时发生错误: onfocus 元素获得焦点: onkeypress某个键盘的键被 ...
- ecshop验证码图片无法显示终极解决办法
ecshop验证码图片无法显示终极解决办法 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2014-06-06 客户在安装好ecshop之后所有前台的证码不显示,后 ...