HDU 1727:Hastiness

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1727


题目翻译:

把数字转换为英文并输出,简单模拟。


AC代码:

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

string s[] = {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
string shiwei[] = {"0","1","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
///如果数字是0~19,使用第一个函数
int main() {
    int number;
    while(~scanf("%d",&number)) {
       string ans = "";
       if(number>=0 && number<20) {  ///如果数字四0~20则直接输出
            ans += s[number];
       }
       else if(number>=20 && number<100) {  ///如果是20~100
           ans += shiwei[number/10];
           number = number%10;
           if(number) {
                ans += "-" + s[number];
           }
       }else if(number>=100 && number<1000) {
            ans = s[number/100] + " hundred";
            number = number%100;
            if(number) {
                if(number>=0 && number<20) {
                    ans += " and " + s[number];
                }
                else {
                    ans += " and " + shiwei[number/10];
                    number = number%10;
                    if(number) {
                        ans += "-"  + s[number];
                    }
                }
            }
       }
       else {  ///是四位数
            ans += s[number/1000] + " thousand";
            number = number%1000;
            if(number) {
                 if(number>=0 && number<20) {
                    ans += " and " + s[number];
                }
                else if(number>=20 && number<100) {
                    ans += " and " + shiwei[number/10];
                    number = number%10;
                    if(number) {
                        ans += "-" + s[number];
                    }
                }else {
                    ans += " and " + s[number/100] + " hundred";
                    number = number%100;
                    if(number) {
                        if(number>=0 && number<20) {
                            ans += " and " + s[number];
                        }
                        else if(number>=20 && number<100) {
                            ans += " and " + shiwei[number/10];
                            number = number%10;
                            if(number) {
                                ans += "-" + s[number];
                            }
                        }
                    }
                }
            }
       }
       cout<<ans<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值