题目大意是这样的,就是:
"one" => 1
"twenty" => 20
"two hundred forty-six" => 246
"seven hundred eighty-three thousand nine hundred and nineteen" => 783919
转换的范围是0到1000,000,注意转换需要支持'and'。
先声明这个不是作业帮哈,我自己完成了已经,也pass了,使用递归的思路做的,按thousand、hundred这种有特殊意义的词分段,之后再转换,不过总感觉不是最好的方法,应该还有更简单的方法吧
求大神给讲解讲解。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
可以直接从左往右过词,
例如
seven hundred eighty-three thousand nine hundred and nineteen
设置
num = 0
读
seven
,则把num
设为7
读
hundred
,则把num
乘以100
,变成700
读
eighty-three
,则把num
加上83
变成783
... 就是遇到读到数字就相加,读到单位就相乘,以此类推
来自:http://stackoverflow.com/a/12014376 ,我修改下支持“and”
最终综合各种版本在我原来基础上改的,可能还是有点乱,不过比以前好多了。
其实这个问题确实按一楼那种思路来着,但是千万别忘记hundred这个词比较特殊,因为它既可以修饰量词,比如thousand这种的,又可以当做量词,比如three thousand,所以不能一棒子打死,大概就这样。
由于只能采纳一个,我就采纳有明的了,其他的我都点赞了。