大家一起来编程第十一期

题目:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值