简体   繁体   English

表示数字序列的算法

[英]Algorithm to represent a sequence of numbers

I have a sequence of numbers to generate, and I want to generate it using some sort of algorithm (iterative or recursive, doesn't matter). 我有一个要生成的数字序列,我想使用某种算法(迭代或递归,无关紧要)生成它。

Contextualizing: This numbers are indexes to iterative over a list of lists. 情境化:此数字是在列表列表上进行迭代的索引。 I need to do a permutation (combination, i don't know exactly), but I need to generate all combinations of all positions of that list of lists. 我需要做一个排列(组合,我不清楚),但是我需要生成列表的所有位置的所有组合。

The sequence and the output I am trying to get is: 我想获得的序列和输出是:

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

and so on... the last state is: 依此类推...最后一个状态是:

1 5
2 5
3 5
4 5
5 5

Note that at each line break is a step of iteration or recursion. 请注意,在每个换行处都是迭代或递归的步骤。 The algorithm must be generic. 该算法必须是通用的。 This code that i wrote can help, but it isn't what I want. 我写的这段代码可以提供帮助,但这不是我想要的。 :( :(

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');
    }
}

I hope that you can help me ! 希望你能帮助我! (: (:

You can do it like this: 您可以这样做:

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;
}

The sequence of the second digits is stored in the array "creatively" named second . 第二个数字的序列“创造性地”存储在名为second的数组中。 The do / while loop "increments" this array as if it were a base-5 number stored as five separate digits. do / while循环将该数组“递增”,就好像它是一个以5为底的数字存储为五个单独的数字。

Here is a demo on ideone . 这是有关ideone演示

Based on comments below by the venerable Eric Lippert, edits for the OPs original intent: 根据尊敬的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); will give the output the OP originally asked for. 将给出OP最初要求的输出。

Old Answer 旧答案

What you're looking for is called a Cartesian Product . 您要寻找的是笛卡尔积 LINQ is your friend: 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);

EDIT: Just for fun, here's a way to do N-Ary cartesian products. 编辑:只是为了好玩,这是一种做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;
}

And now you can get what you had before with: 现在,您可以使用之前的功能:

var p = NAryCartesianProduct(5, 2);

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

I'm sure there's a more efficient way than creating new arrays all of the time but I just hacked this up quick :) 我敢肯定有一种比一直创建新数组更有效的方法,但是我只是很快地破解了它:)

Here's a much more informative answer on this: Generating all Possible Combinations 这是一个更有用的答案: 生成所有可能的组合

EDIT2: Apparently the original link is the origination of the answer from that SO post. EDIT2:显然,原始链接是该SO帖子答案的来源。 I didn't read through to the end until now. 直到现在我都没有读到最后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM