【PAT】1082. Read Number in Chinese (25)
2015-10-22 09:28
197 查看
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
yi Shi Wan ling ba Bai
分析:要注意0的转换,如0, 10, 100, 108, 1008, 100008 等情况。
代码如下:
#include <iostream>
#include <vector>
#include <string>
#include <stack>
using namespace std;
void toChinese(vector<string> &v){
v.push_back("ling");
v.push_back("yi");
v.push_back("er");
v.push_back("san");
v.push_back("si");
v.push_back("wu");
v.push_back("liu");
v.push_back("qi");
v.push_back("ba");
v.push_back("jiu");
}
void setOther(vector<string> &v){
v.push_back("Shi");
v.push_back("Bai");
v.push_back("Qian");
v.push_back("Wan");
}
int main(int argc, char** argv) {
vector<string> chinese;
toChinese(chinese);
vector<string> other; //进制
setOther(other);
int i, num, cnt, t;
string str;
cin>>str;
if( (str.size()==1&&str[0]=='0') || (str.size()==2 && str[0]=='-' && str[1]=='0') ){
printf("ling\n");
return 0;
}
stack<string> sta;
cnt = 0;
t = 0;
for(i=str.size()-1; i>=0; i--){
t++;
num = str[i]-'0';
if(t==9){
//亿
sta.push("Yi");
sta.push(chinese[num]);
continue;
}
if(str[i]=='-'){
sta.push("Fu");
break;
}
if(num != 0){
if(cnt >= 1){
sta.push(other[cnt-1]);//进制
}
sta.push(chinese[num]);//数字
}else{
if(sta.empty() || sta.top()=="ling"){
if(!sta.empty() && sta.top()=="ling" && cnt==4){
sta.push("Wan");
}
}else{
if(sta.top()!="Wan")
sta.push("ling");
}
}
if(cnt == 4){
cnt=0;
}
cnt++;
}
cout<<sta.top();
sta.pop();
while(!sta.empty()){
cout<<" "<<sta.top();
sta.pop();
}
cout<<endl;
return 0;
}
("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的转换,如0, 10, 100, 108, 1008, 100008 等情况。
代码如下:
#include <iostream>
#include <vector>
#include <string>
#include <stack>
using namespace std;
void toChinese(vector<string> &v){
v.push_back("ling");
v.push_back("yi");
v.push_back("er");
v.push_back("san");
v.push_back("si");
v.push_back("wu");
v.push_back("liu");
v.push_back("qi");
v.push_back("ba");
v.push_back("jiu");
}
void setOther(vector<string> &v){
v.push_back("Shi");
v.push_back("Bai");
v.push_back("Qian");
v.push_back("Wan");
}
int main(int argc, char** argv) {
vector<string> chinese;
toChinese(chinese);
vector<string> other; //进制
setOther(other);
int i, num, cnt, t;
string str;
cin>>str;
if( (str.size()==1&&str[0]=='0') || (str.size()==2 && str[0]=='-' && str[1]=='0') ){
printf("ling\n");
return 0;
}
stack<string> sta;
cnt = 0;
t = 0;
for(i=str.size()-1; i>=0; i--){
t++;
num = str[i]-'0';
if(t==9){
//亿
sta.push("Yi");
sta.push(chinese[num]);
continue;
}
if(str[i]=='-'){
sta.push("Fu");
break;
}
if(num != 0){
if(cnt >= 1){
sta.push(other[cnt-1]);//进制
}
sta.push(chinese[num]);//数字
}else{
if(sta.empty() || sta.top()=="ling"){
if(!sta.empty() && sta.top()=="ling" && cnt==4){
sta.push("Wan");
}
}else{
if(sta.top()!="Wan")
sta.push("ling");
}
}
if(cnt == 4){
cnt=0;
}
cnt++;
}
cout<<sta.top();
sta.pop();
while(!sta.empty()){
cout<<" "<<sta.top();
sta.pop();
}
cout<<endl;
return 0;
}
相关文章推荐
- 数据库链接字符串查询网站
- Flex字符串比较 还有Flex字符串操作
- Ruby中创建字符串的一些技巧小结
- ASP下经常用的字符串等函数参考资料
- 将字符串小写转大写并延时输出的批处理代码
- 将字符串转换成System.Drawing.Color类型的方法
- Lua源码中字符串类型的实现
- Lua性能优化技巧(四):关于字符串
- 字符串聚合函数(去除重复值)
- Ruby中的字符串编写示例
- 总结的5个C#字符串操作方法分享
- sqlserver中求字符串中汉字的个数的sql语句
- sql server字符串非空判断实现方法
- VBS的字符串及日期操作相关函数
- C#实现将千分位字符串转换成数字的方法
- jquery 删除字符串最后一个字符的方法解析
- PowerShell实现在字符串中查找大写字母
- PowerShell中使用Out-String命令把对象转换成字符串输出的例子
- PowerShell中字符串使用单引号和双引号的区别
- Powershell小技巧之获取字符串的行数