0

答えが持つべき次元の量によってパラメーター化されたスパイラルのジェネレーターを作成しようとしています。

2 次元 (x, y) の例

static void caller()
{
  for (int t = 0; t < 10; t++)
  for (int x = 0; x <= t; x++)
  {
     int y = (t-x);
     printAllPossibleSigns(0, x, y);
  }
}

3 次元 (x、y、z) の例

static void caller()
{
  for (int t = 0; t < 10; t++)
  for (int x = 0; x <= t; x++)
  for (int y = 0; y <= (t-x); y++)
  {
     int z = (t-x-y);
     printAllPossibleSigns(0, x, y, z);
  }
}

4 次元 (x、y、z、アルファ) の例

static void caller()
{
  for (int t = 0; t < 10; t++)
  for (int x = 0; x <= t; x++)
  for (int y = 0; y <= (t-x); y++)
  for (int z = 0; z <= (t-x-y); z++)
  {
     int alpha = (t-x-y-z);
     printAllPossibleSigns(0, x, y, z, alpha);
  }
}

ただし、一度に 1 つの結果 (または結果のバッチ) のみを生成しようとしています。

したがって、イテレータに使用したい場合、どのように正確に行う必要があるので、それを使用すると、1 つの呼び出しnext()の「結果」を取得する必要があります。printAllPossibleSigns(0, ...);

for-loops入力として与えるの束と;の場合に値をt保持する配列を置き換えるメソッドがあれば、すでに十分でしょう。の場合に値を保持します。などの場合の値。xx, yx, yx, y, zx, y, zx, y, z, alpha

私の質問が十分に明確であることを願っています。

4

1 に答える 1

2

わかりました、失速する代わりに、int で機能する解決策があります。一般的な解決策ははるかに難しいことに注意してください。これはボックス内で「スパイラル」します。

public static void main(String... ignored) {
    caller(10, 7, new Callback<int[]>() {
        @Override
        public void on(int[] ints) {
            System.out.println(Arrays.toString(ints));
        }
    });
}

interface Callback<T> {
    public void on(T t);
}

public static void caller(int maxSum, int dimensions, Callback<int[]> callback) {
    int[] ints = new int[dimensions];
    for (int t = 0; t < maxSum; t++) {
        caller(t, 0, ints, callback);
    }
}

private static void caller(int sum, int idx, int[] ints, Callback<int[]> callback) {
    if (idx == ints.length) {
        callback.on(ints);
        return;
    }
    for (int i = 0; i < sum; i++) {
        ints[idx] = i;
        caller(sum - i, idx+1, ints, callback);
    }
}

版画

[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 1, 0, 0]
[0, 0, 0, 1, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 2]
[0, 0, 0, 0, 0, 1, 1]
[0, 0, 0, 0, 0, 2, 0]
[0, 0, 0, 0, 1, 0, 1]
[0, 0, 0, 0, 1, 1, 0]
[0, 0, 0, 0, 2, 0, 0]
[0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 1, 0, 1, 0]
[0, 0, 0, 1, 1, 0, 0]
[0, 0, 0, 2, 0, 0, 0]

...

[7, 0, 1, 0, 0, 0, 1]
[7, 0, 1, 0, 0, 1, 0]
[7, 0, 1, 0, 1, 0, 0]
[7, 0, 1, 1, 0, 0, 0]
[7, 0, 2, 0, 0, 0, 0]
[7, 1, 0, 0, 0, 0, 1]
[7, 1, 0, 0, 0, 1, 0]
[7, 1, 0, 0, 1, 0, 0]
[7, 1, 0, 1, 0, 0, 0]
[7, 1, 1, 0, 0, 0, 0]
[7, 2, 0, 0, 0, 0, 0]
[8, 0, 0, 0, 0, 0, 1]
[8, 0, 0, 0, 0, 1, 0]
[8, 0, 0, 0, 1, 0, 0]
[8, 0, 0, 1, 0, 0, 0]
[8, 0, 1, 0, 0, 0, 0]
[8, 1, 0, 0, 0, 0, 0]
[9, 0, 0, 0, 0, 0, 0]
于 2013-08-06T09:00:10.483 に答える