题意:
输入一个九位整数,输出它的汉字读法(用拼音表示)。
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;
}
原文地址:https://www.cnblogs.com/ldudxy/p/11865893.html