题目:
36块砖,36人搬,男搬4,女搬3,小孩2人搬一砖,问:男、女、小孩给若干?
我的回答:
这个实际上就是另一个百鸡问题,多元方程问题。使用多重循环就可以解出。这个问题只有单解。
语言:C#
编辑器:SharpDevelop 1.03
环境:Windows Server 2003, .Net Framework 1.1
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2004-4-27
* Time: 12:06
*
* 36块砖,36人搬,男搬4,女搬3,小孩2人搬一砖,问:男、女、小孩给若干?
*/
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
namespace _6
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private ArrayList Result;
public void Caculate()
{
int i,j,k;
for (i=0;i<37;i++){
for(j=0;j<36;j++){
k=36-i-j;
if (k%2!=0) continue;
if ((4*i+3*j+0.5*k)==36){
Result.Add("男:"+i.ToString()+
";女:"+j.ToString()+
";孩:"+k.ToString());
}
}
}
return ;
}
public MainForm()
{
InitializeComponent();
Result = 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.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 56);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(288, 216);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(368, 168);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 32);
this.button1.TabIndex = 1;
this.button1.Text = "Start";
this.button1.Click += new System.EventHandler(this.Button1Click);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(536, 365);
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)
{
Caculate();
textBox1.Lines = (string[]) Result.ToArray(typeof(string));
}
}
}