学英语/华为机试(C/C++)

题目描述

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

如22:twenty two,123:one hundred and twenty three。

 

说明:

数字为正整数,长度不超过九位,不考虑小数,转化结果为英文小写;

输出格式为twenty two;

非法数据请返回“error”;

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

方法原型:public static String parse(long num) 

输入描述:

输入一个long型整数

输出描述:

输出相应的英文写法

示例1

输入

2356

输出

two thousand three hundred and fifty six

代码1:调试了好久

//第四十一题  学英语
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string sP[9]{ "billion","","million","","","thousand","hundred","and" };
	string sNum[28]{"zero", "one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen",
	"fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety" };
	string inStr;
	while (cin >> inStr)
	{
		size_t iLength = inStr.length();
		if (iLength > 9)
		{
			cout << "error" << endl;
			continue;
		}
		for (int i = iLength-1; i >= 0; i--)
		{
			int temp = iLength - i - 1;
			if (iLength > 2 && i == 1)
			{
				if ((inStr[temp] - '0') && (inStr[temp-1] - '0'))
                {
                    cout << sP[9 - i - 1] << " " 
                        << sNum[(inStr[temp] - '0') > 1 ? (inStr[temp] - '0' + 18) : (inStr[temp] - '0' + 9+inStr[temp+1] - '0')] << " ";
                    
                }
				else if(inStr[temp] - '0')
					cout << sNum[(inStr[temp] - '0') > 1 ? (inStr[temp] - '0' + 18) : (inStr[temp] - '0' + 9)] << " ";
				else
					cout << sP[9 - i - 1] << " ";
			}			
			else if (iLength > 2 && i > 1)
			{
				if (i != 4 && i != 5)
				{
					if((inStr[temp] - '0'))
						cout << sNum[inStr[temp] - '0'] << " " << sP[9 - i - 1] << " ";
					else if(i==3 && (inStr[temp - 1] - '0'))
						cout << sP[9 - i - 1] << " ";
				}
					
				else if(i==4 && (inStr[temp] - '0'))
				    cout << sNum[inStr[temp] - '0' + 18] << " ";
				else if (i == 5)
				{
					if(inStr[temp] - '0')
						cout << sNum[inStr[temp] - '0'] << " " << sP[6] << " " << sP[7] << " ";
					else
						cout << sP[6] << " " << sP[7] << " ";
				}		
			}
			else if (iLength > 2 && i < 1)
			{
				if ((inStr[temp] - '0') &&((inStr[temp - 1] - '0')!=1))
					cout << sNum[inStr[temp] - '0'];
			}
				
		}
		cout << endl;
	}
	return 0;
}

代码2:非本人所写,看看其它人怎么写的

#include<iostream>
#include<string>
#include<vector>
 
using namespace std;
 
string arr1[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
string arr2[] = { "ten", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nighteen" };
string arr3[] = { "","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety" };
string arr4[] = { "and","hundred"};
string arr5[] = {"","thousand","million","billion"};
 
string convert(int num)
    {
    string s;
    if(num>0&&num<=9)
        {
        s+=arr1[num];
    }
    else if(num>=10&&num<=19)
        {
        s+=arr2[num%10];
    }
    else if(num>=20&&num<=99)
        {
        if(num%10==0)
            {
            s+=arr3[num/10];
        }
        else
            {
            s+=arr3[num/10]+' '+arr1[num%10];
        }
    }
    else if(num>=100&&num<=999)
        {
        if(num%100==0)
            {
            s+=arr1[num/100]+' '+arr4[1];
        }
        else if(num%100<=9)
            {
            s+=arr1[num/100]+' '+arr4[1]+' '+arr4[0]+' '+arr1[num%100];
        }
        else
            {
            s+=arr1[num/100]+' '+arr4[1]+' '+arr4[0]+' '+convert(num%100);
        }
    }
    return s;
}
 
int main()
{
    long num;
    while (cin >> num)
        {
        string res;
        vector<int> v;
        while(num)
            {
            v.push_back(num%1000);
            num=num/1000;
        }
        for(int i=v.size()-1;i>=0;i--)
            {
            res+=convert(v[i]);
            if(i!=0)
                {
                res+=' ';
            }
            res+=arr5[i];
            if(i!=0)
                {
                res+=' ';
            }
        }
        cout<<res<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值