将固定数量的for循环转换为参数化数量的问题 [英] Trouble with converting fixed-amount of for loops to parametrized amount

查看:87
本文介绍了将固定数量的for循环转换为参数化数量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使螺旋生成器通过答案应具有的尺寸参数化.

I am trying to make a generator for Spirals being parametrized by the amount of dimensions the answer should have.

二维(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,alpha)的示例

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个结果(或一批结果):

However now I am trying to generate only 1 result (or batch of results) at once:

如果我想将其用于迭代器,那么现在需要怎么做,因此使用next()它将检索一次printAllPossibleSigns(0, ...);调用的结果".

So how exactly would I need to do it now if I want to use it for an iterator, so using the next() it should retrieve 'the result' of one printAllPossibleSigns(0, ...); call.

如果有某种方法可以代替一堆for-loops就足够了,其中我给出了t作为输入,而在x, y情况下给出了一个保存x值的数组;在x, y, z的情况下保持x, y值; x, y, z, alpha值(如果是x, y, z, alpha等)

It would be enough already if there would be some method replacing the bunch of for-loops in which I give as input the t and an array holding the x-value in case of x, y; holding the x, y-value in case of x, y, z; the x, y, z-value in case of x, y, z, alpha, etc.

我希望我的问题很清楚.

I hope my question is clear enough.

推荐答案

好吧,除了停顿之外,还有一种解决方案可以对int起作用,但一般的解决方案要困难得多,请注意:这将螺旋式"出现在盒子中.

Ok, instead of stalling there is a solution which will work for ints, a general solution is much harder, note: This will "spiral" out in boxes.

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]

这篇关于将固定数量的for循环转换为参数化数量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆