题目背景
(本道题目隐藏了两首歌名,找找看哪~~~)
《爱与愁的故事第一弹·heartache》第二章。
经历了心痛后,爱与愁大神不行了。
题目描述
月落乌啼问爱与愁大神为什么,爱与愁大神写了一个数字 $n$($n \le 9999$),说翻译成英语就知道为什么了。月落乌啼接过这个数字后,本想翻译成英语,但是班主任叫他去帮个忙。他想尽快知道答案,于是这个艰巨的任务就拜托你了。
标准美式英语,仅在末两位 0时加 `and` 且没有连字符。
输入格式
只有一行,一个数 n(n <= 9999)
输出格式
一行英文,表示 n 翻译成英语的答案。
样例 #1
样例输入 #1
5208
样例输出 #1
five thousand two hundred and eight
样例 #2
样例输入 #2
5280
样例输出 #2
five thousand two hundred eighty
样例 #3
样例输入 #3
5000
样例输出 #3
five thousand
样例 #4
样例4输入 #4
5028
样例输出 #4
five thousand and twenty eight
提示
输出行初不大写。按标准美式英语输出,即两个非零的数中间没零则不加 `and`,行末无句号。
#############################################################################
思路:
首先我们将四位数分为1、2、3、4位数这几种可能,然后将数字拆分到b数组中,这样可以方便使用,还需要将数字的位数找出来,最后进行分类讨论,中间间隔着0的输出and,其他的直接输出就行。
#include<bits/stdc++.h>
using namespace std;
int a,n,m,b[10];
string c[10050];
int main()
{
c[0]="zero";
c[1]="one";
c[2]="two";
c[3]="three";
c[4]="four";
c[5]="five";
c[6]="six";
c[7]="seven";
c[8]="eight";
c[9]="nine";
c[10]="ten";
c[11]="eleven";
c[12]="twelve";
c[13]="thirteen";
c[14]="fourteen";
c[15]="fifteen";
c[16]="sixteen";
c[17]="seventeen";
c[18]="eighteen";
c[19]="nineteen";
c[20]="twenty";
c[30]="thirty";
c[40]="forty";
c[50]="fifty";
c[60]="sixty";
c[70]="seventy";
c[80]="eighty";
c[90]="ninety";
c[100]="hundred";
c[1000]="thousand";
cin>>n;
m=n;
while(1)
{
a++;
m/=10;
if(m==0)
break;
}
m=n;
for(int i=a;i>=1;i--)
{
b[i]=m%10;
m/=10;
}
if(a==1)
{
cout<<c[n];
}
if(a==2)
{
if(n%10==0)
{
cout<<c[n];
}
else
if(n>=20)
{
cout<<c[b[1]*10]<<" "<<c[n%10];
}
else
{
cout<<c[n];
}
}
m=n;
if(a==3)
{
cout<<c[b[1]]<<" "<<c[100]<<" ";
if(b[2]==0&&n%100!=0)
{
cout<<"and"<<" "<<c[b[3]]<<" ";
exit(0);
}
if(m%100>=10)
{
m%=100;
if(m%10==0||m<=20)
{
cout<<c[m]<<" ";
exit(0);
}
else
{
cout<<c[b[2]*10]<<" "<<c[b[3]]<<" ";
}
}
}
if(a==4)
{
cout<<c[b[1]]<<" "<<c[1000]<<" ";
if(n%1000==0) exit(0);
if(b[2]!=0)
{
cout<<c[b[2]]<<" "<<c[100]<<" ";
if(b[3]==0&&b[4]!=0)
{
cout<<"and"<<" "<<c[b[4]]<<" ";
exit(0);
}
if(b[3]!=0&&b[4]==0)
{
cout<<c[b[3]*10]<<" ";
exit(0);
}
if(b[3]!=0&&b[4]!=0)
{
if(b[3]<=2)
{
cout<<c[b[3]*10+b[4]]<<" ";
exit(0);
}
else
{
cout<<c[b[3]*10]<<" "<<c[b[4]]<<" ";
exit(0);
}
}
}
if(b[2]==0)
{
cout<<"and"<<" ";
if(b[3]<=2)
{
cout<<c[b[3]*10+b[4]]<<" ";
exit(0);
}
else
{
cout<<c[b[3]*10]<<" "<<c[b[4]]<<" ";
exit(0);
}
}
}
return 0;
}
最后,动动各位发财的小手,点个赞吧。