题目:
从0.1.2.3.4.5.6.7.8.9共10个数中,如何计算出其中6个数字的组合数
我的回答:
关于排列组合的算法有很多,其中多重循环法最不可取。比较好的有回朔法等,我使用的是下面的算法。这个算法并非是我的原创,而是来自于网上。我不清楚最初的出处,没有引用原文请原作者见了莫怪。
组合算法
本程序的思路是开一个数组,其下标表示1到m个数,数组元素的值为1表示其下标
代表的数被选中,为0则没选中。
首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。
然后从左到右扫描数组元素值的“10”组合,找到第一个“10”组合后将其变为
“01”组合,同时将其左边的所有“1”全部移动到数组的最左端。
当第一个“1”移动到数组的m-n的位置,即n个“1”全部移动到最右端时,就得
到了最后一个组合。
例如求5中选3的组合:
1 1 1 0 0 //1,2,3
1 1 0 1 0 //1,2,4
1 0 1 1 0 //1,3,4
0 1 1 1 0 //2,3,4
1 1 0 0 1 //1,2,5
1 0 1 0 1 //1,3,5
0 1 1 0 1 //2,3,5
1 0 0 1 1 //1,4,5
0 1 0 1 1 //2,4,5
0 0 1 1 1 //3,4,5
我的具体程序如下:
编译环境: Windows Server 2003
Microsoft .Net Framework 1.1.4322
SharpDevelop 1.03.1768
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2005-4-18
* Time: 15:31
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
namespace zuhe
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label1;
public ArrayList Zuhe ;
public string BuildString(int[] a)
{
string s="";
for (int i=0;i<10;i++){
if (a==1){
s=s+i.ToString()+"-";
}
}
return s.Substring(0,s.Length-1) ;
}
public int Combination(int m,int n)
{
int i,j,count=0,k,l;
bool end = true;
int[] a = new int[10];
for (i=0;i<10;i++)
{
if (i<n)a=1;
else a=0;
}
Zuhe.Clear();
while (end)
{
k=l=0;
for(i=0;i<m-1;i++){
if (a==1)
{
l++;
if(a[i+1]==0)
{
count++;
Zuhe.Add(BuildString(a));
a=0;
a[i+1]=1;
k=1;
l-=1;
for(j=0;j<i;j++)
{
if (j<l)
a[j]=1;
else
a[j]=0;
}
break;
}
}
}
if (k==0)
{
Zuhe.Add(BuildString(a));
count++;
end =false;
}
}
return count;
}
public MainForm()
{
InitializeComponent();
Zuhe = new ArrayList();
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
this.label1 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(376, 232);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(136, 32);
this.label1.TabIndex = 3;
this.label1.Text = "label1";
//
// label3
//
this.label3.Location = new System.Drawing.Point(376, 344);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(136, 32);
this.label3.TabIndex = 5;
this.label3.Text = "label3";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(8, 40);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox2.Size = new System.Drawing.Size(320, 320);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(376, 88);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(136, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(384, 168);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 24);
this.button1.TabIndex = 1;
this.button1.Text = "Start";
this.button1.Click += new System.EventHandler(this.Button1Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(376, 288);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(136, 32);
this.label2.TabIndex = 4;
this.label2.Text = "label2";
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(536, 389);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "MainForm";
this.Text = "MainForm";
this.ResumeLayout(false);
}
#endregion
void Button1Click(object sender, System.EventArgs e)
{
int i ;
//Datetime s = new DateTime();
label1.Text =@"Time1="+
Convert.ToString(DateTime.Now.Hour)+":"+
Convert.ToString(DateTime.Now.Minute)+":"+
Convert.ToString(DateTime.Now.Second)+":"+
Convert.ToString(DateTime.Now.Millisecond);
textBox1.Text = Combination(10,6).ToString();
label2.Text =@"Time2="+
Convert.ToString(DateTime.Now.Hour)+":"+
Convert.ToString(DateTime.Now.Minute)+":"+
Convert.ToString(DateTime.Now.Second)+":"+
Convert.ToString(DateTime.Now.Millisecond);
for (i=0; i<Convert.ToInt16( textBox1.Text );i++)
textBox2.Lines = (string[])Zuhe.ToArray(typeof(string)) ;
label3.Text =@"Time3="+
Convert.ToString(DateTime.Now.Hour)+":"+
Convert.ToString(DateTime.Now.Minute)+":"+
Convert.ToString(DateTime.Now.Second)+":"+
Convert.ToString(DateTime.Now.Millisecond);
}
}
}
//时间紧,没有注释,望大家原谅。