本文主要是介绍东方博宜1126:英文翻译,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目描述
请将一个数字,翻译成对应的英文。
输入
一个自然数 n。(0≤n≤2^31−1)
输出
输出这个数的英文,最后不要有多余的空格。
输入样例:
1111111111
输出样例:
one billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven
代码实现:
#include<iostream>
#include<cmath>
using namespace std;
int list[4]={1000000000,1000000,1000,100};
char s[4][10]={"billion","million","thousand","hundred"};
void read_1(int n)
{switch(n){case 1:cout<<"one ";break;case 2:cout<<"two ";break;case 3:cout<<"three ";break;case 4:cout<<"four ";break;case 5:cout<<"five ";break;case 6:cout<<"six ";break;case 7:cout<<"seven ";break;case 8:cout<<"eight ";break;case 9:cout<<"nine ";break;case 10:cout<<"ten ";break;case 11:cout<<"eleven ";break;case 12:cout<<"twelve ";break;case 13:cout<<"thirteen ";break;case 14:cout<<"fourteen ";break;case 15:cout<<"fifteen ";break;case 16:cout<<"sixteen ";break;case 17:cout<<"seventeen ";break;case 18:cout<<"eighteen ";break;case 19:cout<<"nineteen ";break;default:break;}
}
void read_10(int n)
{int t=n/10;if(t>1){switch(t){case 2:cout<<"twenty ";break;case 3:cout<<"thirty ";break;case 4:cout<<"forty ";break;case 5:cout<<"fifty ";break;case 6:cout<<"sixty ";break;case 7:cout<<"seventy ";break;case 8:cout<<"eighty ";break;case 9:cout<<"ninty ";break; }n%=10;read_1(n);}else read_1(n);
}
void Read(int n)
{if(n<100){read_10(n);return;}int i=0;while(n>0&&i<4){int t=n/list[i];if(t>0){Read(t);cout<<s[i]<<" ";}n%=list[i];i++;}if(i==4&&n>0){cout<<"and ";read_10(n);}return;
}
int main()
{int n;cin>>n;Read(n);return 0;
}
这篇关于东方博宜1126:英文翻译的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!