爱与愁的一千个伤心的理由
时空限制 1000ms / 128mb
题目描述
月落乌啼问爱与愁大神为什么,爱与愁大神写了一个数字n(n<=9999),说翻译成英语就知道为什么了。月落乌啼接过这个数字后,本想翻译成英语,但是班主任叫他去帮个忙。他想尽快知道答案,于是这个艰巨的任务就拜托你了。
标准美式英语,仅在末两位<=10时加and且没有连字符。
输入输出格式
输入格式:只有一行,一个数n(n<=9999)
一行英文,表示n翻译成英语的答案
输入输出样例
输入样例1
5208
输出样例1
five thousand two hundred and eight
输入样例2
5280
输出样例2
five thousand two hundred eighty
输入样例3
5000
输出样例3
five thousand
解题思路:
先打表,要注意还有11~19这几个特殊的数,然后分别得到输入的数值的个位、十位、百位、千位,模拟着输出就可以了。这里解释一下一些特殊情况:①输出千位时,百位是0而十位或个位不为0,要输出“and” ②输出百位时,十位是0而个位不为0,要输出“and” ③输出十位时,要特判是否属于11~19,如果不是的话,就输出十位对应的英文,再判断个位是否大于0,大于0的话再加个空格,最后特判一下输入是0的情况,搞定。
#include<iostream> #include<cstdio> #include<string> using namespace std; int main(){ string ch[105]; ch[0]="zero"; ch[1]="one"; ch[2]="two"; ch[3]="three"; ch[4]="four"; ch[5]="five"; ch[6]="six"; ch[7]="seven"; ch[8]="eight"; ch[9]="nine"; ch[10]="ten"; ch[11]="eleven"; ch[12]="twelve"; ch[13]="thirteen"; ch[14]="fourteen"; ch[15]="fifteen"; ch[16]="sixteen"; ch[17]="seventeen"; ch[18]="eighteen"; ch[19]="nineteen"; ch[20]="twenty"; ch[30]="thirty"; ch[40]="forty"; ch[50]="fifty"; ch[60]="sixty"; ch[70]="seventy"; ch[80]="eighty"; ch[90]="ninety"; int num; cin>>num; int d,c,b,a; d=num%10;num/=10; c=num%10;num/=10; b=num%10;num/=10; a=num%10; if(a>0){ cout<<ch[a]<<" "<<"thousand "; if(b==0&&(c>0||d>0))cout<<"and "; } if(b>0){ cout<<ch[b]<<" "<<"hundred "; if(c==0&&(d>0))cout<<"and "; } if(c>0){ if(c==1){ cout<<ch[c*10+d]; } else cout<<ch[c*10]; if(d>0) cout<<" "; } if(d>0)cout<<ch[d]; if(a==0&&b==0&&c==0&&d==0)cout<<ch[0]; return 0; }