牛客-华为机试-较难-字符串-HJ42学英语

描述

Jessi初学英语,为了快速读出一串数字,编写程序将数字转换成英文:

具体规则如下:
1.在英语读法中三位数字看成一整体,后面再加一个计数单位。从最右边往左数,三位一单位,例如12,345 等
2.每三位数后记得带上计数单位 分别是thousand, million, billion.
3.公式:百万以下千以上的数 X thousand X, 10亿以下百万以上的数:X million X thousand X, 10 亿以上的数:X billion X million X thousand X. 每个X分别代表三位数或两位数或一位数。
4.在英式英语中百位数和十位数之间要加and,美式英语中则会省略,我们这个题目采用加上and,百分位为零的话,这道题目我们省略and

下面再看几个数字例句:

22: twenty two

100:  one hundred

145:  one hundred and forty five
1,234:  one thousand two hundred and thirty four
8,088:  eight thousand (and) eighty eight (注:这个and可加可不加,这个题目我们选择不加)
486,669:  four hundred and eighty six thousand six hundred and sixty nine
1,652,510:  one million six hundred and fifty two thousand five hundred and ten

说明:
数字为正整数,不考虑小数,转化结果为英文小写;
保证输入的数据合法

关键字提示:and,billion,million,thousand,hundred。

数据范围:1≤n≤2000000

输入描述:

输入一个long型整数

输出描述:

输出相应的英文写法

示例1

输入:

22

输出:

twenty two

代码

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool check(long int num){
    string s=to_string(num);
    if(s.size()>13)
        return false;
    if(num<0)
        return false;
    for(int i=0;i<s.size();i++){
        if(s.size()>1&&s[s.size()-1]==0)
            return false;
        if(!(s[i]>='0'&&s[i]<='9'))
            return false;
    }
    return true;
}
const vector<string> s1={"","one","two","three","four","five","six","seven","eight","nine"};//个位
const vector<string> s2={"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};//十位为1的+个位,两位数的表达
const vector<string> s3={"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};//十位为2,3,4,5,6,7,8,9
const vector<string> s4={"","thousand","million","billion"};//千,百万,十亿
int main(){
    long int num;
    while(cin>>num){
        if(check(num)==false)
            cout<<"error"<<endl;
        else{
            vector<string> res;
            string s=to_string(num);
            vector<string> threebit;
            int i=s.size()-1;
            while(i>=0){
                int j=i;
                while(j>0&&i-j<2)
                    j--;
                threebit.push_back(s.substr(j,i-j+1));
                i=j-1;
            }
            reverse(threebit.begin(),threebit.end());
            for(int i=0;i<threebit.size();i++){
                if(threebit[i].size()==3){//该部分有3位数
                    if(threebit[i][0]-'0'){
                        res.push_back(s1[threebit[i][0]-'0']);
                        res.push_back("hundred");
                        if((threebit[i][1]-'0')||(threebit[i][2]-'0'))
                            res.push_back("and");
                    }
                    if(threebit[i][1]-'0'==1)
                        res.push_back(s2[threebit[i][2]-'0']);
                    else{
                        res.push_back(s3[threebit[i][1]-'0']);
                        res.push_back(s1[threebit[i][2]-'0']);
                    }
                }
                else if(threebit[i].size()==2){//该部分有2位数
                    if(threebit[i][0]-'0'==1)
                        res.push_back(s2[threebit[i][1]-'0']);
                    else{
                        res.push_back(s3[threebit[i][0]-'0']);
                        res.push_back(s1[threebit[i][1]-'0']);
                    }
                }
                else//该部分只有1位数
                    res.push_back(s1[threebit[i][0]-'0']);
                res.push_back(s4[threebit.size()-i-1]);
            }
            int x;
            for(auto &t:res)//自动对
                if(t!="")
                    cout<<t<<' ';
            cout<<endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

画弋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值