0

我有一系列数字要生成,我想使用某种算法(迭代或递归,没关系)来生成它。

上下文化:这个数字是迭代列表列表的索引。我需要做一个排列(组合,我不确切知道),但我需要生成该列表列表的所有位置的所有组合。

我想要得到的序列和输出是:

1 1
2 1
3 1
4 1
5 1

1 2
2 1
3 1
4 1
5 1

1 3
2 1
3 1
4 1
5 1

1 4
2 1
3 1
4 1
5 1

1 5
2 1
3 1
4 1
5 1

1 1
2 2
3 1
4 1
5 1

1 2
2 2
3 1
4 1
5 1

1 3
2 2
3 1
4 1
5 1

1 4
2 2
3 1
4 1
5 1

1 5
2 2
3 1
4 1
5 1

1 1
2 3
3 1
4 1
5 1

1 2
2 3
3 1
4 1
5 1

1 3
2 3
3 1
4 1
5 1

1 4
2 3
3 1
4 1
5 1

1 5
2 3
3 1
4 1
5 1

1 1
2 4
3 1
4 1
5 1

依此类推……最后一个状态是:

1 5
2 5
3 5
4 5
5 5

请注意,在每个换行符处都是一个迭代或递归步骤。算法必须是通用的。我编写的这段代码可以提供帮助,但这不是我想要的。:(

List<List<int>> lstDays = new List<List<int>>
{
    new List<int>{1,2,3,4,5}, //day 18
    new List<int>{1,2,3,4,5}, //day 19
    new List<int>{1,2,3,4,5}, //day 22
    new List<int>{1,2,3,4,5}, //day 23
    new List<int>{1,2,3,4,5}, //day 24
};

for(int i=0;i<lstDays.Count;i++)
{
    for(int j=0;j<lstDays[i].Count;j++)
    {
        for(int k=0;k<lstDays.Count;k++)
        {
            Console.Write(k+1);

            //Console.Write(j+1);

            Console.Write('\n');
        }
        Console.Write('\n');
    }
}

我希望你能帮助我!(:

4

2 回答 2

2

你可以这样做:

int[] second = new[] {0,0,0,0,0};
bool finish = false;
while (true) {
    for (int i = 0 ; i != 5 ; i++) {
        Console.WriteLine("{0} {1}", i+1, second[i]+1);
    }
    Console.WriteLine();
    int p = 0;
    do {
        second[p]++;
        if (second[p] == 5) {
            second[p] = 0;
            p++;
        } else {
            break;
        }
    } while (p != 5);
    if (p == 5) break;
}

第二个数字的序列存储在“创造性地”命名的数组中seconddo/循环“递增”这个while数组,就好像它是一个以 5 为基数的数字,存储为五个单独的数字。

这是关于 ideone 的演示

于 2013-08-25T14:41:35.570 回答
1

根据尊敬的 Eric Lippert 的以下评论,对 OP 的原始意图进行了编辑:

public void OutputSequence(int length){
    Recurse(length-1, Enumerable.Range(1, length).ToArray(), new int[length]);  
}

public void Recurse(int position, int[] arr, int[] state){  
    if (position == -1){
        PrintState(state);  
        return;
    }

    for (int i = 0; i < arr.Length; i++)
    {           
        state[position] = arr[i];
        Recurse(position-1, arr, state);
    }
}

public void PrintState(int[] state){
    for (int i = 0; i < state.Length; i++)
        Console.WriteLine ("{0} {1}",i+1, state[i]);        

        Console.WriteLine ();
}

OutputSequence(5);将给出 OP 最初要求的输出。

旧答案

您要查找的内容称为笛卡尔积。LINQ 是你的朋友:

var pairs = from i in Enumerable.Range(1, 5)
            from j in Enumerable.Range(1, 5)
            select new {i, j};

foreach(var p in pairs)
    Console.WriteLine ("{0} {1}", p.i, p.j);

编辑:只是为了好玩,这是一种做 N-Ary 笛卡尔积的方法。

public IEnumerable<IEnumerable<int>> NAryCartesianProduct(int upper, int times){
    if (times == 0)
        return Enumerable.Empty<IEnumerable<int>>();

    var nums = Enumerable.Range(1, upper);          
    IEnumerable<IEnumerable<int>> products = nums.Select(i => new[]{i});

    for (int i = 1; i < times; i++)
    {
        products = from p in products
                   from n in nums
                   select p.Concat(new [] {n});                                     
    }       

    return products;
}

现在你可以得到你以前拥有的东西:

var p = NAryCartesianProduct(5, 2);

foreach(var i in p)
    Console.WriteLine (i);

我确信有一种比一直创建新数组更有效的方法,但我只是快速解决了这个问题:)

这里有一个更丰富的答案:生成所有可能的组合

EDIT2:显然原始链接是该SO帖子的答案的起源。直到现在我才读到最后。

于 2013-08-25T15:00:28.367 回答