问题:假设你有6个配料来制作比萨饼。您可以制作5种顶级披萨的多少种变种?
例如:
[意大利辣香肠,培根,菠萝,洋葱,蘑菇,辣椒] [意大利辣香肠,培根,菠萝,洋葱,蘑菇,奶酪] 等...
在开发过程中,我已经练习了3个可能的选项,分别为6个,因为我知道总共有三个组合,每个组合有两个元素,这使得我的多维数组很容易生成。
public class SystematicList {
static String options[] = {
"Pepperoni",
"Bacon",
"Cheese"
};
public static void main(String[] args) {
/**
* Generate a multi-dimensional array to represent the combinations of
* pizza toppings we can possibly have from the available <code>options</code>
*
*/
String[][] combos = new String[3][2];
/**
* Ideally what we would like to create.
* combos[0][0] = "Pepperoni";
* combos[0][1] = "Bacon";
* combos[1][0] = "Pepperoni";
* combos[1][1] = "Cheese";
* combos[2][0] = "Bacon";
* combos[2][1] = "Cheese";
*/
for (int pizzas = 0; pizzas < combos.length; pizzas++) {
for (int toppings = 0; toppings < combos[pizzas].length; toppings++) {
combos[pizzas][toppings] = options[0]; // <<< issue : element.
System.out.print(" " + combos[pizzas][toppings]);
}
System.out.println("");
}
/*
* Current Output :
* run:
* Pepperoni Bacon
* Pepperoni Bacon
* Pepperoni Bacon
* BUILD SUCCESSFUL (total time: 0 seconds)
*
* ^ this is obvious, as I currently do not know how I'll select an
* element from the array of topping options before/after a specified
* index [example: 0 which would than range 1 - 2 apposed to 0 - 2
* thus 'dropping' an optional element, making this much easier.
*
* Loop is virtually only doing the actions of :
* 0 : 1,2
* 1 : 1,2
* 2 : 1,2
*/
}
}
我决定生成一个2D列表来存储值。
int [#available combination] [#possible values]
我当前的代码假设我们已经知道可能的组合(当然我们当然没有),并且无论我们总是可以确定值(可以构建多少5,4,3,2个顶部比萨饼)
我如何从options []中选择一个元素,同时确保没有其他数组已包含您尝试插入的元素。我会试过Arrays.contains或Arrays.equals但是我不知道要插入什么来比较,combos [pizzas-1]?
if (!Arrays.contains(combos[pizzas], combos[pizzas-1]) {
combos[pizzas][toppings] = options[?];
}
答案 0 :(得分:0)
一种方法是仅查看尚未考虑的配料。因此,例如当从[意大利辣香肠,培根,菠萝,洋葱,蘑菇,辣椒,奶酪]中选择三种配料时,您可能决定跳过奶酪,然后选择辣椒,然后跳过蘑菇和洋葱,并选择菠萝和培根。当然,您需要留下足够的配料来完成披萨,因此您不能跳过奶酪,辣椒,蘑菇,洋葱和菠萝。 JavaScript中的示例代码使用递归算法为您提供了一个想法。
function combos(toppings, count) {
var result = [];
helper(result, [], toppings, toppings.length, count);
return result;
}
function helper(result, selection, toppings, length, count) {
if (count == 0) // no more toppings left to add
result.push(selection); // this must be a solution
else
// start at --count because we need to be able to add other toppings
for (var i = --count; i < length; i++)
// add the selected topping to the selection
// then consider toppings we haven't looked at yet
helper(result, selection.concat([toppings[i]]), toppings, i, count);
}
答案 1 :(得分:0)
我不会使用数组来保存披萨选项。您有两列浇头。如果我想要一块普通的奶酪比萨怎么办?
这是一个使用递归来构建不同数量浇头的比萨饼的解决方案。
public static List<String> getPizzas( List<String> ingredients )
{
if( ingredients.isEmpty() )
{
List<String> pizzas = new ArrayList<String>();
String plainPizza = "";
pizzas.add( plainPizza );
return pizzas;
}
else if( ingredients.size() == 1 )
{
List<String> pizzas = new ArrayList<String>();
String plainPizza = "";
String oneToppingPizza = ingredients.get( 0 );
pizzas.add( plainPizza );
pizzas.add( oneToppingPizza );
return pizzas;
}
else
{
List<String> pizzas = new ArrayList<String>();
for( String onePizza : getPizzas( ingredients.subList( 1, ingredients.size() )))
{
String pizzaWithoutTopping = onePizza;
String pizzaWithTopping = ingredients.get( 0 ) + " " + onePizza;
pizzas.add( pizzaWithTopping );
pizzas.add( pizzaWithoutTopping );
}
return pizzas;
}
}
public static void main( String[] args )
{
String options[] = { "Pepperoni", "Bacon", "Cheese" };
for( String onePizza : getPizzas( Arrays.asList( options ) ) )
{
System.out.println( onePizza );
}
}