HDU 1727 Hastiness

题目链接

题目意思

给你一个不大于四位数的数字,输出它的英文写法。

解题思路

就是一道暴力题。不过很容易出错,要考虑的情况比较多。所以做题的时候得认真一点。

代码部分
#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值