题解 | #学英语#

学英语

https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc

#include <iostream>
using namespace std;

string ones[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
string tens[] = { "ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen" };
string twenties[] = { "zero","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety" };
string hundreds[] = { "hundred", "thousand", "million", "billion" };

string translate(int y,int pos)
{
    string strOut;
    int hundred = y/100;
    int left = y%100;
    if(hundred > 0)
    {   
        strOut = ones[hundred] + " " + hundreds[0];
    }

    if(left > 0)
    {
        if(!strOut.empty())
            strOut += " and ";
        if(left >= 10 &&left < 20)
        {
            strOut  += tens[left-10];
        }
        else if(left < 10)
        {
            strOut  += ones[left];
        }
        else 
        {
           int ten = left/10;
           int one = left%10;
           strOut  += twenties[ten];

           if(one > 0)
           {
            strOut  += " "  + ones[one];
           }
        }
    }
    return strOut;
};
int main() {
    long a;
    while (cin >> a) { // 注意 while 处理多个 case
        string strOut;
        int pos = 0;
        int x = a, y = a%1000;

        do 
        {
            if(pos >0)
            {
                if(strOut.empty())
                {
                     strOut = hundreds[pos];
                }
                else 
                {
                    strOut = hundreds[pos] + " " + strOut;
                }
                
            }
                
            string out = translate(y,pos);
            if(!out.empty())
            {
                if(strOut.empty())
                {
                    strOut = out;
                }
                else 
                {
                    strOut = out + " " + strOut;
                }    
            }        
             ++pos;
             x = x/1000;
             y = x%1000;
        }while (x > 0);
       cout<<strOut<<endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

08-05 14:06
门头沟学院 Java
驼瑞驰_招募评论官版...:哈哈哈,hr怪实诚的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务