当我想出这个问题时,我一直在测试我的班级。我尝试了一切,但似乎我找不到任何解决方案。这是我的pseudo code:
String[] pastaRecipe = {a, b, c, d, e};
String[] saladRecipe = {f, g, h, i, j};
String[][] finalRecipes = {pastaRecipe, saladRecipe};
String[] testInput = {a, b, c, d, e};
if (pastaRecipe contansAll testInput) then
print You coulds cook a pasta
end
这是我的实施:
public class RecipeTest() {
public static void main(String[] args) {
try {
ArrayList<String> results = new ArrayList<>();
String[] porkSteak = {"Pork", "Garlic", "Dried bay leaves", "Vinegar", "Soy sauce", "Whole pepper corn", "Salt"};
String[] porkSoup = {"Pork", "String Beans", "Vinegar", "Soy sauce", "Whole pepper corn", "salt"};
String[][] finalRecipes = {porkSteak, porkSoup};
String[] testInput = {"Pork", "Garlic", "Dried bay leaves", "Vinegar", "Soy sauce", "Whole pepper corn", "Salt"};
for (int i = 0; i < finalRecipes.length; i++) {
if(Arrays.asList(testInput).containsAll(Arrays.asList(finalRecipes[i]))) {
results.add(finalRecipes[i][finalRecipes.length - 1]);
}
}
System.out.println("You can cook " + results.get(0));
}
}
我的输出: 你可以煮盐
预期产量: 你可以煮猪肉。
答案 0 :(得分:1)
要使其工作量最少,您必须创建一个配方名称数组,并使其与finalRecipes
数组保持同步。 E.g。
String[][] finalRecipes = {porkSteak, porkSoup};
String[] recipeNames = {"Pork Steak", "Pork Soup"};
然后在if
if (Arrays.asList(testInput).containsAll(Arrays.asList(finalRecipes[i]))) {
results.add(recipeNames[i]);
}
但理想情况下,您希望创建一个Recipe
类,其中包含成分列表及其名称。