# 大家一起来编程第十期

从0.1.2.3.4.5.6.7.8.9共10个数中,如何计算出其中6个数字的组合数

“01”组合，同时将其左边的所有“1”全部移动到数组的最左端。

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

Microsoft .Net Framework 1.1.4322
SharpDevelop  1.03.1768

/*
* Created by SharpDevelop.
* 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++;

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)
{
count++;
end =false;

}

}
return count;
}

public MainForm()
{
InitializeComponent();
Zuhe = new ArrayList();
}

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

}
}

//时间紧，没有注释，望大家原谅。

• 评论

• 上一篇
• 下一篇