码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 486 - English-Number Translator

时间:2014-12-02 22:40:16      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:io   ar   os   sp   for   on   bs   as   nbsp   

题目:给你一个数字的英文写法,翻译成阿拉伯数字(没有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;
}


UVa 486 - English-Number Translator

标签:io   ar   os   sp   for   on   bs   as   nbsp   

原文地址:http://blog.csdn.net/mobius_strip/article/details/41683647

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!