嘿,伙计们,我试图在Java中创建一个系统的列表生成器,但是我遇到了轻微的错误。我们的例子是披萨。
问题:假设你总共有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[# possible combination][# possible values]
我当前的代码假设我们已经知道可能的组合(尽管我们当然不知道),并且无论如何我们总是可以确定值(可以制作多少个5、4、3、2个披萨)
我如何能够从options[]中选择一个元素,同时确保没有其他数组已经包含你试图插入的那些元素。我会尝试数组。包含或数组。但是我不知道插入什么来比较,combos[pizzas-1] ?
if (!Arrays.contains(combos[pizzas], combos[pizzas-1]) {
combos[pizzas][toppings] = options[?];
}
一种方法是只看你还没有考虑过的配料。举个例子,当你从[意大利辣香肠、培根、菠萝、洋葱、蘑菇、辣椒、奶酪]中选择三种配料时,你可能会决定跳过奶酪,然后选择辣椒,然后跳过蘑菇和洋葱,选择菠萝和培根。当然,你需要留下足够的配料来完成你的披萨,所以你不能跳过奶酪、辣椒、蘑菇、洋葱和菠萝。示例代码在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);
}
我不会使用数组来保存披萨选项。有两列是配料。如果我想要一个纯奶酪披萨呢?
这是一个使用递归构建不同数量配料的披萨的解决方案。
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 );
}
}