UVa 486 - English-Number Translator

题目:给你一个数字的英文写法,翻译成阿拉伯数字(没有and)。

分析:模拟,递归。这个可以用很多方法求解吧。

            这里利用递归,将数字分成几个部分的和(只考虑百万,千,万),分别求解相加即可。

说明:(⊙_⊙)。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

char buf[20][10];

char number[32][10] = {
	"negative", "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", "hundred", 
	"thousand", "million"}; 

int value[32] = {
	-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
	20,30,40,50,60,70,80,90,100,1000,1000000};
	
int find(char *str)
{
	for (int i = 1 ; i < 29 ; ++ i)
		if (!strcmp(str, number[i]))
			return value[i];
	return -1;
}

int dfs(int a, int b)
{
	if (a > b) return 0;
	if (a == b) return find(buf[a]);
	else {
		int million = 0,thousand = 0,hundred = 0,sum = 0;
		for (int i = a ; i <= b ; ++ i) {
			if (!strcmp(buf[i], "million")) million = i;
			if (!strcmp(buf[i], "thousand")) thousand = i;
			if (!strcmp(buf[i], "hundred")) hundred = i;
			sum += find(buf[i]);
		}
		if (million) return dfs(a, million-1)*1000000+dfs(million+1, b);	
		if (thousand) return dfs(a, thousand-1)*1000+dfs(thousand+1, b);
		if (hundred) return dfs(a, hundred-1)*100+dfs(hundred+1, b);
		return sum;
	}
}

int main()
{
	int count = 0;
	while (scanf("%s",buf[count ++]) != EOF) {
		while (getchar() != '\n')
			scanf("%s",buf[count ++]);
		
		if (!strcmp(buf[0], "negative"))
			printf("-%d\n",dfs(1, count-1));
		else printf("%d\n",dfs(0, count-1));
		count = 0;
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值