东方博宜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
#include
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<0)
	{
		cout<<"and ";
		read_10(n);
	}
	return;
}
int main()
{
	int n;
	cin>>n;
	Read(n);
	return 0;
}

你可能感兴趣的:(算法,c++)