PAT甲级1082 Read Number in Chinese (25 分)

Posted ldudxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT甲级1082 Read Number in Chinese (25 分)相关的知识,希望对你有一定的参考价值。

题意:

输入一个九位整数,输出它的汉字读法(用拼音表示)。

trick:

字符串数组""其实会输出一个空格,而不是什么都不输出,导致测试点0和4格式错误。

代码:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int num[17];
string united[17]={"","yi","er","san","si","wu","liu","qi","ba","jiu"};
string unit[17]={"","","Shi","Bai","Qian","Wan","Shi","Bai","Qian"};
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int x;
cin>>x;
if(x<0){
cout<<"Fu ";
x=-x;
}
else if(x==0){
cout<<"ling";
return 0;
}
int tamp=1;
for(int i=1;i<=8;++i)
tamp*=10;
for(int i=9;i;--i){
num[i]=x/tamp;
x%=tamp;
tamp/=10;
}
int space=0;
if(num[9])
cout<<united[num[9]]<<" Yi",space=1;
int pos=0;
for(int i=8;i;--i){
if(num[i]){
pos=i;
break;
}
}
int ling=0;
if(num[9]&&pos<8)
ling=1;
for(int i=pos;i;--i){
if(num[i]){
if(space)
cout<<" ",space=0;
if(ling)
cout<<"ling",ling=0,space=1;
if(space)
cout<<" ",space=0;
cout<<united[num[i]],space=1;
if(i!=5&&i!=1){
if(space)
cout<<" ",space=0;
cout<<unit[i],space=1;
}
}
else{
if(!ling&&i!=5)
ling=1;
}
if(i==5){
if(space)
cout<<" ",space=0;
cout<<"Wan",space=1;
}
}
return 0;
}

以上是关于PAT甲级1082 Read Number in Chinese (25 分)的主要内容,如果未能解决你的问题,请参考以下文章

PAT 1082. Read Number in Chinese

1082. Read Number in Chinese (25)字符串处理——PAT (Advanced Level) Practise

PAT (Advanced Level) 1082. Read Number in Chinese (25)

1082 Read Number in Chinese (25 分)

1082. Read Number in Chinese (25)

1082. Read Number in Chinese (25)