我正在寻找一种将“数字单词”按数字顺序排序的方法。例如,假设我有列表[“五”,“三”,“六”,“八”],我想将其排序为[“三”,“五”,“六”,“八”]而不是字典顺序。我环顾四周,包括这个网站,最接近/最好的解决方案是使用地图并根据键值对对列表进行排序。
我想出了与此相似的其他想法,但最后所有想法都需要建立一个大表/列表来匹配数字与实际数字。如果可能的话,我想要一个可以自动扩展的解决方案 - 所以如果它适用于上面的列表,它也适用于列表[“十万亿九千五百二十”,“六”,“四” ]。我越是想到它,我就越不确定它是否可以完成。非常感谢任何帮助。
答案 0 :(得分:0)
您可以使用以下函数转换数字,然后使用任何标准排序算法对数字进行排序。
public static int convertNum(String input)
{
ArrayList<String> prefixTable = new ArrayList<String>();
prefixTable.add("ZE");
prefixTable.add("ON");
prefixTable.add("TW");
prefixTable.add("TH");
prefixTable.add("FO");
prefixTable.add("FI");
prefixTable.add("SI");
prefixTable.add("SE");
prefixTable.add("EI");
prefixTable.add("NI");
prefixTable.add("TEN");
prefixTable.add("ELEVEN");
prefixTable.add("TWELVE"); //MUST BE AT END OTHERWISE TW REGISTERS AS PREFIX
Map<String, Integer> delimTable = new HashMap<String, Integer>();
delimTable.put("HUNDRED", 100);
delimTable.put("THOUSAND", 1000);
delimTable.put("MILLION", 1000000);
delimTable.put("BILLION", 1000000000);
String[] theWords = input.toUpperCase().split(" ");
int runningTotal = 0;
int currentRunningNum = 0;
int currentNum = 1;
boolean skipTransfer = false;
for(int i = 0; i < theWords.length; i++)
{
if(theWords[i].equals("AND") || theWords[i].equals("NEGATIVE"))
continue;
if(delimTable.containsKey(theWords[i]))
{
if(delimTable.get(theWords[i]) == 100)
skipTransfer = true;
else
skipTransfer = false;
currentRunningNum *= delimTable.get(theWords[i]);
continue;
}
if(!skipTransfer)
{
runningTotal += currentRunningNum;
currentRunningNum = 0;
}
skipTransfer = false;
currentNum = 1;
for(int j = prefixTable.size() - 1; j >= 0 && currentNum == 1; j--)
if(theWords[i].startsWith(prefixTable.get(j)))
currentNum *= j;
if(theWords[i].endsWith("EEN"))
currentNum += 10;
if(theWords[i].endsWith("TY"))
{
currentNum *= 10;
skipTransfer = true;
}
currentRunningNum += currentNum; //ADD TOTAL
}
runningTotal += currentRunningNum;
if(theWords[0].equals("NEGATIVE"))
runningTotal *= -1;
return runningTotal;
}
答案 1 :(得分:0)
也许尝试这个答案:https://stackoverflow.com/a/26951693/3663023。当我试图解决这个确切的问题时,它对我有用。正如@abagshaw所提到的,在将单词形式的数字转换为数字形式之后,使用任何排序算法对数组进行排序将起作用。
@Kartic的代码如下:
boolean isValidInput = true;
long result = 0;
long finalResult = 0;
List<String> allowedStrings = Arrays.asList
(
"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","billion","trillion"
);
String input="One hundred two thousand and thirty four";
if(input != null && input.length()> 0)
{
input = input.replaceAll("-", " ");
input = input.toLowerCase().replaceAll(" and", " ");
String[] splittedParts = input.trim().split("\\s+");
for(String str : splittedParts)
{
if(!allowedStrings.contains(str))
{
isValidInput = false;
System.out.println("Invalid word found : "+str);
break;
}
}
if(isValidInput)
{
for(String str : splittedParts)
{
if(str.equalsIgnoreCase("zero")) {
result += 0;
}
else if(str.equalsIgnoreCase("one")) {
result += 1;
}
else if(str.equalsIgnoreCase("two")) {
result += 2;
}
else if(str.equalsIgnoreCase("three")) {
result += 3;
}
else if(str.equalsIgnoreCase("four")) {
result += 4;
}
else if(str.equalsIgnoreCase("five")) {
result += 5;
}
else if(str.equalsIgnoreCase("six")) {
result += 6;
}
else if(str.equalsIgnoreCase("seven")) {
result += 7;
}
else if(str.equalsIgnoreCase("eight")) {
result += 8;
}
else if(str.equalsIgnoreCase("nine")) {
result += 9;
}
else if(str.equalsIgnoreCase("ten")) {
result += 10;
}
else if(str.equalsIgnoreCase("eleven")) {
result += 11;
}
else if(str.equalsIgnoreCase("twelve")) {
result += 12;
}
else if(str.equalsIgnoreCase("thirteen")) {
result += 13;
}
else if(str.equalsIgnoreCase("fourteen")) {
result += 14;
}
else if(str.equalsIgnoreCase("fifteen")) {
result += 15;
}
else if(str.equalsIgnoreCase("sixteen")) {
result += 16;
}
else if(str.equalsIgnoreCase("seventeen")) {
result += 17;
}
else if(str.equalsIgnoreCase("eighteen")) {
result += 18;
}
else if(str.equalsIgnoreCase("nineteen")) {
result += 19;
}
else if(str.equalsIgnoreCase("twenty")) {
result += 20;
}
else if(str.equalsIgnoreCase("thirty")) {
result += 30;
}
else if(str.equalsIgnoreCase("forty")) {
result += 40;
}
else if(str.equalsIgnoreCase("fifty")) {
result += 50;
}
else if(str.equalsIgnoreCase("sixty")) {
result += 60;
}
else if(str.equalsIgnoreCase("seventy")) {
result += 70;
}
else if(str.equalsIgnoreCase("eighty")) {
result += 80;
}
else if(str.equalsIgnoreCase("ninety")) {
result += 90;
}
else if(str.equalsIgnoreCase("hundred")) {
result *= 100;
}
else if(str.equalsIgnoreCase("thousand")) {
result *= 1000;
finalResult += result;
result=0;
}
else if(str.equalsIgnoreCase("million")) {
result *= 1000000;
finalResult += result;
result=0;
}
else if(str.equalsIgnoreCase("billion")) {
result *= 1000000000;
finalResult += result;
result=0;
}
else if(str.equalsIgnoreCase("trillion")) {
result *= 1000000000000L;
finalResult += result;
result=0;
}
}
finalResult += result;
result=0;
System.out.println(finalResult);
}
}