Skip to content

12757: 阿尔法星人翻译官

implementation, http://cs101.openjudge.cn/practice/12757

阿尔法星人为了了解地球人,需要将地球上所有的语言转换为他们自己的语言,其中一个小模块是要将地球上英文表达的数字转换为阿尔法星人也理解的阿拉伯数字。 请你为外星人设计这个模块,即给定一个用英文表示的整数,将其转换成用阿拉伯数字表示的整数。这些数的范围从-999,999,999到+999,999,999。 下列单词是你的程序中将遇到的所有有关数目的英文单词:

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

输入

输入一行,由几个表示数目的英文单词组成(长度不超多200)。注意:负号将由单词negative表示。 当数的大小超过千时,并不用完全单词hundred表示。例如1600将被写为"one thousand six hundred", 而不是"sixteen hundred"。

输出

输出一行,表示答案。

样例输入

negative seven hundred twenty nine

样例输出

-729

其他参考样例: six :6 one million one hundred one:1000101 eight hundred fourteen thousand twenty two:814022

同学们在没有测试数据的情况下,发现了后台测试数据的问题。联系OpenJudge管理员,答复结果:

1)数据里没有one thousand thousand 这样的。

2)原先有一组数据不是特别好nine hundred thirteen million six hundred fifty one thousand thirty eight hundred ten,改为nine hundred thirteen million six hundred fifty one thousand eight hundred ten

这题就恶心在 hundred 可能在千和百万之前,所以要暂时储存出现的一百,等后边出现千或百万 的时候就好处理了。

英语中数字是三位为一个周期念的,故每次碰到 thousand、million 就乘一下加到数字中去。

python
tokens = [str(i) for i in input().split()]
dic={"zero":0, "one":1, "two":2, "three":3, "four":4, "five":5, "six":6, 
     "seven":7, "eight":8, "nine":9, "ten":10, "eleven":11, "twelve":12, 
     "thirteen":13, "fourteen":14, "fifteen":15, "sixteen":16, "seventeen":17, 
     "eighteen":18, "nineteen":19, "twenty":20, "thirty":30, "forty":40, 
     "fifty":50, "sixty":60, "seventy":70, "eighty":80, "ninety":90, 
     "hundred":100, "thousand":1000, "million":1000000}

sign = 1
if tokens[0]=="negative":
    sign = -1
    del tokens[0]

total = 0
tmp = 0
for i in tokens:
    if i in ("thousand", "million"):
        total += tmp*dic[i]
        tmp = 0
        continue
    if i == "hundred":
        tmp *= dic[i]
    else:
        tmp += dic[i]
        
print( sign * (total + tmp) )

2021fall-cs101,李佳霖。对上面程序解读:可以把hundred,thousand,million 这些看成是计量单位,而其他的具体的数当作系数。由于这道题不需要考虑如one thousand million 而只存在one hundred million 这样的情况,因此可以把thousand 和million 看成是一类。t 作为累计计数单位,对具体的数字进行加和处理。而遇到hundred 时便进行释放,成100 处理;遇到thousand 和million则需要考虑前面是否存在hundred million 这样的情况,并做对应的加和处理。最终输出带有正负号的数字。

2021fall-cs101,龚靖淞。one thousand million就是one billion来着。

2021fall-cs101,侯勇启。思路:用字典实现即可,易知不会出现thousand million 这样的数据,只会存在hundred million 和hundred thousand 数据,从而每次遍历到thousand、million 都可以结算;用cnt 滚动记录阶段值,结算后清零。

递归实现

python
# 焦玮宸 24数学科学学院
dictionary = {'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'ten': 10, 'eleven': 11, 'twelve': 12, 'thirteen': 13, 'fourteen': 14, 'fifteen': 15, 'sixteen': 16, 'seventeen': 17, 'eighteen': 18, 'nineteen': 19, 'twenty': 20, 'thirty': 30, 'forty': 40, 'fifty': 50, 'sixty': 60, 'seventy': 70, 'eighty': 80, 'ninety': 90}
def convert(words):
    if words[0] == "negative":
        return -convert(words[1:])
    if "million" in words:
        ind = words.index("million")
        return convert(words[:ind]) * (10 ** 6) + (convert(words[ind + 1:]) if ind < len(words) - 1 else 0)
    if "thousand" in words:
        ind = words.index("thousand")
        return convert(words[:ind]) * (10 ** 3) + (convert(words[ind + 1:]) if ind < len(words) - 1 else 0)
    if "hundred" in words:
        ind = words.index("hundred")
        return convert(words[:ind]) * (10 ** 2) + (convert(words[ind + 1:]) if ind < len(words) - 1 else 0)
    return sum(list(map(lambda s: dictionary[s], words)))


print(convert(list(input().split())))