题目意思
给你一个不大于四位数的数字,输出它的英文写法。
解题思路
就是一道暴力题。不过很容易出错,要考虑的情况比较多。所以做题的时候得认真一点。
代码部分
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
map<int, string> M;
map<int, string> M1;
map<int, string> M2;
void init()
{
M[1] = "one";
M[2] = "two";
M[3] = "three";
M[4] = "four";
M[5] = "five";
M[6] = "six";
M[7] = "seven";
M[8] = "eight";
M[9] = "nine";
M[0] = "zero";
M1[1] = "ten";
M1[2] = "twenty";
M1[3] = "thirty";
M1[4] = "forty";
M1[5] = "fifty";
M1[6] = "sixty";
M1[7] = "seventy";
M1[8] = "eighty";
M1[9] = "ninety";
M2[0] = "ten";
M2[1] = "eleven";
M2[2] = "twelve";
M2[3] = "thirteen";
M2[4] = "fourteen";
M2[5] = "fifteen";
M2[6] = "sixteen";
M2[7] = "seventeen";
M2[8] = "eighteen";
M2[9] = "nineteen";
}
int main()
{
int t;
while(~scanf("%d",&t))
{
init();
if(t<10)///个位数
cout<<M[t]<<endl;
else if(t>=10&&t<20)///十位数
cout<<M2[t-10]<<endl;
else if(t>=20&&t<100)
{
if(t%10==0)
cout<<M1[t/10]<<endl;
else
cout<<M1[t/10]<<"-"<<M[t%10]<<endl;
}
else if(t>=100&&t<1000)///百位数
{
if(t%100==0)
cout<<M[t/100]<<" hundred"<<endl;
else
{
if(t%100<10)
cout<<M[t/100]<<" hundred and "<<M[t%100]<<endl;
else if(t%100>=10&&t%100<20)
cout<<M[t/100]<<" hundred and "<<M2[t%100-10]<<endl;
else
{
if((t%100)%10==0)
cout<<M[t/100]<<" hundred and "<<M1[t%100/10]<<endl;
else
cout<<M[t/100]<<" hundred and "<<M1[t%100/10]<<"-"<<M[t%100%10]<<endl;
}
}
}
else///千位数
{
if(t%1000==0)
cout<<M[t/1000]<<" thousand"<<endl;
else
{
if(t%1000<10)
cout<<M[t/1000]<<" thousand and "<<M[t%1000]<<endl;
else if(t%1000>=10&&t%1000<20)
cout<<M[t/1000]<<" thousand and "<<M2[t%1000-10]<<endl;
else if(t%1000>=20&&t%1000<100)
{
if((t%1000)%10==0)
cout<<M[t/1000]<<" thousand and "<<M1[(t%1000)/10]<<endl;
else
cout<<M[t/1000]<<" thousand and "<<M1[t%1000/10]<<"-"<<M[t%1000%10]<<endl;
}
else
{
if((t%1000)%100==0)
cout<<M[t/1000]<<" thousand and "<<M[t%1000/100]<<" hundred"<<endl;
else
{
if((t%1000)%100<10)
cout<<M[t/1000]<<" thousand and "<<M[(t%1000)/100]<<" hundred and "<<M[t%1000%100]<<endl;
else if((t%1000)%100>=10&&(t%1000)%100<20)
cout<<M[t/1000]<<" thousand and "<<M[(t%1000)/100]<<" hundred and "<<M2[t%1000%100-10]<<endl;
else
{
if(((t%1000)%100)%10==0)
cout<<M[t/1000]<<" thousand and "<<M[(t%1000)/100]<<" hundred and "<<M1[((t%1000)%100)/10]<<endl;
else
cout<<M[t/1000]<<" thousand and "<<M[(t%1000)/100]<<" hundred and "<<M1[((t%1000)%100)/10]<<"-"<<M[((t%1000)%100)%10]<<endl;
}
}
}
}
}
}
return 0;
}