找到5个数字的所有组合 [英] Find all combinations of 5 numbers

查看:85
本文介绍了找到5个数字的所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!


我有一个关于如何找到这5个数字的所有可能组合的问题。 (组合必须总是5个数字,如下例所示)


我不想使用嵌套循环。我知道该怎么做。因为真正的函数将有30个数字,我不想嵌套30个循环。


我们怎么能这样做?

列表与LT;字符串> list1 = new List< String>(); 
list1.Add(" 1");
list1.Add(" 2");
list1.Add(" 3");
list1.Add(" 4");
list1.Add(" 5");

// 1组合为:1,2,3,4,5
// 2组合为:2,3,4,5,1
// 3组合是:3,4,5,1,2
//等等......

解决方案

谢谢!


我发现了什么。这似乎有效,但我确实有这样的数字,这是2个数字,只有这样:


11


12


13


14


15


如何使用以下函数获取上述数字?

 private void button1_Click(object sender,EventArgs e)
{
string word =" 12345" ;;
WordPermuatation("",word);
}

void WordPermuatation(字符串前缀,字符串字)
{
int n = word.Length;
if(n == 0){listBox1.Items.Add(prefix); }
else
{
for(int i = 0; i< n; i ++)
{
WordPermuatation(prefix + word [i],word.Substring (0,i)+ word.Substring(i + 1,n - (i + 1)));
}
}
}




Hello!

I have a question about how to find all possible combinations of those 5 numbers. (The combinations must always be 5 numbers like the example below)

I don't want to use nested loops. I know how to do that. Because the real function will have 30 numbers and I dont want to nest 30 loops.

How can we do this?

 List<String> list1 = new List<String>();
            list1.Add("1");
            list1.Add("2");
            list1.Add("3");
            list1.Add("4");
            list1.Add("5");

            //1 combination is: 1,2,3,4,5
            //2 combination is: 2,3,4,5,1
            //3 combination is: 3,4,5,1,2
            //and so on...

解决方案

Thank you!

I found something. This seems to work but I do have numbers like this which is 2 numeric only like this:

11

12

13

14

15

How can I use the function below for above numbers?

 private void button1_Click(object sender, EventArgs e)
        {
            string word = "12345";
            WordPermuatation("", word);
        }

        void WordPermuatation(string prefix, string word)
        {
            int n = word.Length;
            if (n == 0) { listBox1.Items.Add(prefix); }
            else
            {
                for (int i = 0; i < n; i++)
                {
                    WordPermuatation(prefix + word[i], word.Substring(0, i) + word.Substring(i + 1, n - (i + 1)));
                }
            }
        }



这篇关于找到5个数字的所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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