学英语
描述
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
100
145
1234
8088
486669
1652510
输出:
twenty two
one hundred
one hundred and forty five
one thousand two hundred and thirty four
eight thousand eighty eight
four hundred and eighty six thousand six hundred and sixty nine
one million six hundred and fifty two thousand five hundred and ten
答案:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
const string a0[20]={"one","two","three","four","five","six","seven","eight","nine","ten",
"eleven","twelve","thirteen","fourteen","fifteen","sixteen",
"seventeen","eighteen","nineteen"};
const string b0[8]={"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
void transfer1(int a,int b,vector<string>& ret){
int number=a+b*10;
if(number>=1 && number<=19){
ret.push_back(a0[number-1]);
}
else if(number<100 && number>19){
ret.push_back(b0[b-2]);
if(a!=0){
ret.push_back(a0[a-1]);
}
}
}
void transfer2(int a,int b,int c,vector<string>& ret){
if(a!=0 || b!=0 || c!=0){
if(c!=0){
ret.push_back(a0[c-1]);
ret.push_back("hundred");
if(a!=0 || b!=0){
ret.push_back("and");
transfer1(a, b,ret);
}
}
else transfer1(a, b,ret);
}
}
void transfer3(int a,int b,int c,vector<string>& ret){
if(a!=0 || b!=0 || c!=0){
transfer2(a, b, c,ret);
ret.push_back("thousand");
}
}
void transfer4(int a,int b,int c,vector<string>& ret){
if(a!=0 || b!=0 || c!=0){
transfer2(a, b, c,ret);
ret.push_back("million");
}
}
void transfer5(int a,int b,int c,vector<string>& ret){
if(a!=0 || b!=0 || c!=0){
transfer2(a, b, c,ret);
ret.push_back("billion");
}
}
int main(){
long N;
while(cin>>N){
vector<int> vec1;
vector<string> ret;
long n=N;
while(n>0){
vec1.push_back(n%10);
n/=10;
}
if(N<100){
transfer1(vec1[0], vec1[1],ret);
}
else if(N<1000){
transfer2(vec1[0], vec1[1], vec1[2],ret);
}
else if(N<1000000){
transfer3(vec1[3], vec1[4], vec1[5],ret);
transfer2(vec1[0], vec1[1], vec1[2],ret);
}
else if(N<1000000000){
transfer4(vec1[6], vec1[7], vec1[8],ret);
transfer3(vec1[3],vec1[4],vec1[5],ret);
transfer2(vec1[0], vec1[1], vec1[2],ret);
}
else if(N<1000000000000){
transfer5(vec1[9], vec1[10], vec1[11],ret);
transfer4(vec1[6], vec1[7], vec1[8],ret);
transfer3(vec1[3],vec1[4],vec1[5],ret);
transfer2(vec1[0], vec1[1], vec1[2],ret);
}
for(int i=0;i<ret.size()-1;++i){
cout<<ret[i]<<" ";
}
cout<<ret[ret.size()-1]<<endl;
}
return 0;
}