[英]Create an int array of int arrays that contains all possible sums that add up to a given number
我是Java的新手。 我正在編寫一個Android游戲,我需要生成一個包含所有可能的總和(不包括包含數字2或大於8的數字的組合)的int數組的數組,這些總和等於給定的數字。
例如: ganeratePatterns(5)
必須返回數組
[patternNumber][summandNumber] = value
[0][0] = 5
[1][0] = 1
[1][1] = 1
[1][2] = 1
[1][3] = 1
[1][4] = 1
[2][0] = 3
[2][1] = 1
[2][2] = 1
[3][0] = 4
[3][1] = 1
我已經嘗試過像這樣進行操作: 獲取所有可能的總和加到給定的數字上,但是讓我很難像這樣http://introcs.cs.princeton.edu/java/23recursion/Partition.java。 html
解
int n = 10;
int dimension = 0;
//First we need to count number of posible combinations to create a 2dimensionarray
for(List<Integer> sumt : new SumIterator(n)) {
if(!sumt.contains(2) && sumt.size() < 9) {
dimension++;
}
}
int[][] combinationPattern = new int[dimension][];
int foo = 0;
for(List<Integer> sum : new SumIterator(n)) {
if(!sum.contains(2) && sum.size() < 9) {
System.out.println(sum);
combinationPattern[foo] = toIntArray(sum);
foo++;
}
}
它的工作方式不是100%正確,而且非常漂亮,但是足以滿足我的游戲需求
我從這里使用了SumIterator類SumIterator.class我必須將代碼更改for(int j = n-1; j > n/2; j--) {
改為this for(int j = n-1; j >= n/2; j--) {
因為舊版本不會返回所有組合(例如[5,5]等於10)
而且我曾經使用過toIntArray函數。 我已經在StackOverflow上建立了野兔,但是忘記了一個鏈接,因此這里是來源:
public static int[] toIntArray(final Collection<Integer> data){
int[] result;
// null result for null input
if(data == null){
result = null;
// empty array for empty collection
} else if(data.isEmpty()){
result = new int[0];
} else{
final Collection<Integer> effective;
// if data contains null make defensive copy
// and remove null values
if(data.contains(null)){
effective = new ArrayList<Integer>(data);
while(effective.remove(null)){}
// otherwise use original collection
}else{
effective = data;
}
result = new int[effective.size()];
int offset = 0;
// store values
for(final Integer i : effective){
result[offset++] = i.intValue();
}
}
return result;
}
這不是最漂亮的代碼,但是在修改了您引用的代碼后,它可以滿足您的要求。 它也相當快。 通過遠離遞歸(使用堆棧),並完全避免從字符串到整數的轉換,可以使其變得更快。 我可能會回來編輯這些更改。在我漂亮的過時筆記本電腦上運行,它在5秒鍾內打印了50
分區(共204226個)。
在此代碼中退出partition(N)
, partitions
將保留N
的分區。
首先,它以空格分隔的格式(例如: " 1 1 1"
)構建一個以和表示的字符串表示形式的ArrayList。
然后,它創建一個可以存儲所有結果的二維數組。
如果您有任何疑問,請告訴我!
import java.util.ArrayList;
public class Partition
{
static ArrayList<String> list = new ArrayList<String>();
static int[][] partitions;
public static void partition(int n)
{
partition(n, n, "");
partitions = new int[list.size()][0];
for (int i = 0; i < list.size(); i++)
{
String s = list.get(i);
String[] stringAsArray = s.trim().split(" ");
int[] intArray = new int[stringAsArray.length];
for (int j = 0; j < stringAsArray.length; j++)
{
intArray[j] = Integer.parseInt(stringAsArray[j]);
}
partitions[i] = intArray;
}
}
public static void partition(int n, int max, String prefix)
{
if(prefix.trim().split(" ").length > 8 || (prefix + " ").contains(" 2 "))
{
return;
}
if (n == 0)
{
list.add(prefix);
return;
}
for (int i = Math.min(max, n); i >= 1; i--)
{
partition(n - i, i, prefix + " " + i);
}
}
public static void main(String[] args)
{
int N = 50;
partition(N);
/**
* Demonstrates that the above code works as intended.
*/
for (int i = 0; i < partitions.length; i++)
{
int[] currentArray = partitions[i];
for (int j = 0; j < currentArray.length; j++)
{
System.out.print(currentArray[j] + " ");
}
System.out.println();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.