我在将单词转换为数字时遇到问题,例如
五千六百三十二——5632
如果有人做过,我什至不知道如何开始,请指导我如何做...
一般来说,如果您不必对其进行编码,您将如何做到这一点?在此示例中,您的单词集合是:
Five Thousand Six Hundred Thirty two
我们可以将它们中的每一个转换为数字以获得以下集合:
5 1000 6 100 30 2
从 5 开始(提示:5 < 1000 在 1000 的左侧。这表明...!??)您将遵循哪些步骤来获取数字 5632?
如果号码是
六千三百三十亿五千四百万二十二万三千四?
你能找出某种规则(或者更好的算法)吗?
一旦你将大问题分解为一系列小问题,接下来的战斗就是找到正确的编码方法,以正确解决每个小问题
在这里,我是在 python 中完成的,它会从算法的角度帮助你或其他人。
#!/usr/bin/python
__author__ = 'tomcat'
all = {
"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,
"billion" : 1000000000,
"trillion" : 1000000000000,
"quadrillion" : 1000000000000000,
"quintillion" : 1000000000000000000,
"sextillion" : 1000000000000000000000,
"septillion" : 1000000000000000000000000,
"octillion" : 1000000000000000000000000000,
"nonillion" : 1000000000000000000000000000000
};
spliter = {
"thousand" : 1000,
"million" : 1000000,
"billion" : 1000000000,
"trillion" : 1000000000000,
"quadrillion" : 1000000000000000,
"quintillion" : 1000000000000000000,
"sextillion" : 1000000000000000000000,
"septillion" : 1000000000000000000000000,
"octillion" : 1000000000000000000000000000,
"nonillion" : 1000000000000000000000000000000
};
inputnumber = raw_input("Please enter string number : ");
tokens = inputnumber.split(" ");
result = 0;
partial_result = 0;
for index in range(len(tokens)):
if tokens[index] in spliter :
if partial_result == 0:
partial_result = 1;
partial_result *= all[tokens[index]];
result += partial_result;
partial_result = 0;
else:
if tokens[index] == "hundred" :
if partial_result == 0:
partial_result = 1;
partial_result *= all[tokens[index]];
else:
partial_result += all[tokens[index]];
result += partial_result;
print result;
考虑使用map
. 说five thousand six hundred three ten two
。
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<string,int> digits;
digits["one"] = 1;
digits["two"] = 2;
digits["three"] = 3;
digits["four"] = 4;
digits["five"] = 5;
digits["six"] = 6;
digits["seven"] = 7;
digits["eight"] = 8;
digits["nine"] = 9;
digits["ten"] = 10;
digits["hundred"] = 10;
digits["thousand"] = 1000;
const int num_len = 7;
string num_str[num_len]={"five", "thousand", "six", "hundred", "three", "ten", "two"};
int number = digits[num_str[0]]*digits[num_str[1]] +
digits[num_str[2]]*digits[num_str[3]] +
digits[num_str[4]]*digits[num_str[5]] +
digits[num_str[6]];
cout << number;
}
希望这能给你一些开始:-
#include <iostream>
#include <string>
#include <map>
using namespace std;
map<string, int> reference;
string ones[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
void storeOnes(){
for(int i = 0; i < 11; i++){
reference[ones[i]] = i;
}
}
int main(){
//set up
storeOnes();
string test = "onetwothreetwofour";
string buffer;
for(int i = 0; i < test.length(); i++){
buffer.push_back(test.at(i));
map<string, int>::iterator it = reference.find(buffer);
if(it != reference.end()){
cout << (*it).second;
buffer = "";
}
}
cout << endl << endl;
system("pause");
return 0;
}
另一种方法是通过递归,就像这里所做的那样(在 java 和 c++ 中)
https://github.com/jman27182818/words_to_numbers
大多数代码是关于解析字符串以获得可以递归操作的字符串向量。本质上,算法是
A[] = String array //example {"three","hundred"}
Integer converter(A){
if(length(A) <= 4)
handle_base_case;
//either A only has small values or is of the form
//{"three","billion"}
//if length is greater than 4 the array must have a large value
index = find_first_value_greater_than_100(A);
arrayl = A[1:index-1];
arrayr = A[index+1:A.end()];
return (convert_hundreds(arrayl) * Value_of(A[index])+ converter(arrayr) );
}
其中“convert_hundreds”接受一个字符串数组,每个字符串的值不超过 100(或西班牙语为 1,000)并返回数值。这个算法比前一个算法更占用内存,但我喜欢它,因为它似乎可以更好地推广到其他语言。
这是C++中的一个过程
输入:九千九百九十九
输出:9999
矢量“talika”看起来像这样:9 1000 9 100 9 10 9 1
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
bool endswith(string a, string b)
{
//To check if the word ends with ty eg. thirty, forty
int len = a.length();
string last_two = a.substr(len-2, len-1); //ty
if((last_two.compare(b)) == 0)return true;
return false;
}
int main() {
int t,i,j;
int n,cnt, num;
cin >> t;
getchar();
map<string, int>conv;
vector<int>talika;
conv["one"] = 1;
conv["two"] = 2;
conv["three"] = 3;
conv["four"] = 4;
conv["five"] = 5;
conv["six"] = 6;
conv["seven"] = 7;
conv["eight"] = 8;
conv["nine"] = 9;
conv["ten"] = 10;
conv["eleven"] = 11;
conv["twelve"] = 12;
conv["thirteen"] = 13;
conv["fourteen"] = 14;
conv["fifteen"] = 15;
conv["sixteen"] = 16;
conv["seventeen"] = 17;
conv["eighteen"] = 18;
conv["ninteen"] = 19;
conv["thousand"] = 1000;
conv["hundred"] = 100;
conv["twenty"] = 20;
conv["thirty"] = 30;
conv["forty"] = 40;
conv["fifty"] = 50;
conv["sixty"] = 60;
conv["seventy"] = 70;
conv["eighty"] = 80;
conv["ninety"] = 90;
while(t--)
{
string num_in_word, ongsho;
getline(cin,num_in_word);//get the number in words
stringstream x(num_in_word);
bool sheshe_ty = false;
while(getline(x,ongsho, ' '))
{
num = conv[ongsho];
if(endswith(ongsho,"ty"))
{
talika.push_back(num/10);
talika.push_back(10);
sheshe_ty = true;
continue;
}
talika.push_back(num);
sheshe_ty = false;
}
if(conv[ongsho] != 1000 && conv[ongsho] != 100 && sheshe_ty == false){
talika.push_back(1);
}
num = 0;
for(i=0;i < talika.size();i++)
{
num += talika[i] * talika[i+1];
i++;
}
cout << "The Number: " << num << endl;
talika.clear();
}
return 0;
}