博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——【牛客练习题1002】
阅读量:4541 次
发布时间:2019-06-08

本文共 2007 字,大约阅读时间需要 6 分钟。

题目描述

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".

 

输入描述:

Each input file contains one test case, which gives an integer with no more than 9 digits.

输出描述:

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.

 

输入例子:

-123456789

 

输出例子:

Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

 

 

1 #include 
2 #include
3 #include
4 5 6 using namespace std; 7 8 int main() 9 {10 vector
level = { "Fu","Shi","Bai","Qian" };11 vector
Wei = { "","Wan","Yi" };12 vector
numbers = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };13 vector
res;14 string Num;15 cin >> Num;16 if(Num[0] == '-')//如果是负数17 {18 res.push_back(level[0]);19 Num.erase(0, 1);20 }21 int n = Num.length(); 22 if (n == 1)//如果只有一位,则直接输出即可并结束23 {24 cout << numbers[Num[0] - '0'] << endl;25 return 0;26 }27 int f = 0;28 for (int i = 0; i < n; ++i)29 {30 int a = Num[i] - '0';//取出数字31 int p = (n - i - 1) % 4;//判断是否是4位间隔32 if (a > 0)33 {34 if (f)//中间有零存在35 {36 res.push_back(numbers[0]);37 f = 0;38 }39 res.push_back(numbers[a]);//输入数字40 if (p > 0)//不是各位41 res.push_back(level[p]);//输入位42 }43 else if (p != 0)//当中间有0且不是0不是在个位上44 f = 1;45 if (p == 0 && res[res.size() - 1] != "Yi")//是4位间隔且中间不是全为0,例如100000004,就不用输出wan46 res.push_back(Wei[(n - i) / 4]);47 }48 for (int i = 0; i < res.size() - 1; ++i)49 cout << res[i] << " ";50 cout << res[res.size() - 1] << endl;//最后一位不用输出空格51 return 0;52 }

 

转载于:https://www.cnblogs.com/zzw1024/p/11143884.html

你可能感兴趣的文章
Mongodb 命令及 PyMongo 库的使用
查看>>
div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法(非原创)
查看>>
关于SDWebImage加载高清图片导致app崩溃的问题
查看>>
如何查看方法在哪里被调用
查看>>
HUE的自动化安装部署
查看>>
图片服务器(FastDFS)的搭建
查看>>
myBatis应用
查看>>
RuntimeError: DataLoader worker (pid 18255) is killed by signal: Killed.
查看>>
[PHP] 用AppServ一步到位安装PHP服务器
查看>>
mac brew install redis 报错
查看>>
Work? working!
查看>>
开源收藏
查看>>
scipy插值interpolation
查看>>
C# BackgroundWorker
查看>>
移动对meta的定义
查看>>
leetcode 76. Minimum Window Substring
查看>>
如何用Eclipse打jar包
查看>>
学习是一种投资
查看>>
banking
查看>>
Android笔记(十七) Android中的Service
查看>>