C#实现24点算法源代码

  • 来源: 程序员之家 作者: sevenleaf   2010-06-28/14:26
  •       编写环境VS2005
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    namespace 计算24
    {
        public partial class Form1 : Form
        {
            double A,B,C,D,NumberA,NumberB,NumberC,NumberD;
       
            public void jisuan24(int i)
            {  
                //24种转换
                switch (i)
                { 
                    case 1:
                        A = NumberA; B = NumberB; C = NumberC; D = NumberD;
                        break;
                    case 2:
                        A = NumberA; B = NumberB; D = NumberC; C = NumberD;
                        break;
                    case 3:
                        A = NumberA; C = NumberB; B = NumberC; D = NumberD;
                        break;
                    case 4:
                        A = NumberA; C = NumberB; D = NumberC; B = NumberD;
                        break;
                    case 5:
                        A = NumberA; D = NumberB; B = NumberC; C = NumberD;
                        break;
                    case 6:
                        A = NumberA; D = NumberB; C = NumberC; B = NumberD;
                        break;
                    case 7:
                        B = NumberA; A = NumberB; C = NumberC; D = NumberD;
                        break;
                    case 8:
                        B = NumberA; A = NumberB; D = NumberC; C = NumberD;
                        break;
                    case 9:
                        B = NumberA; C = NumberB; A = NumberC; D = NumberD;
                        break;
                    case 10:
                        B = NumberA; C = NumberB; D = NumberC; A = NumberD;
                        break;
                    case 11:
                        B = NumberA; D = NumberB; A = NumberC; C = NumberD;
                        break;
                    case 12:
                        B = NumberA; D = NumberB; C = NumberC; A = NumberD;
                        break;
                    case 13:
                        C = NumberA; A = NumberB; B = NumberC; D = NumberD;
                        break;
                    case 14:
                        C = NumberA; A = NumberB; D = NumberC; B = NumberD;
                        break;
                    case 15:
                        C = NumberA; B = NumberB; A = NumberC; D = NumberD;
                        break;
                    case 16:
                        C = NumberA; B = NumberB; D = NumberC; A = NumberD;
                        break;
                    case 17:
                        C = NumberA; D = NumberB; A = NumberC; B = NumberD;
                        break;
                    case 18:
                        C = NumberA; D = NumberB; B = NumberC; A = NumberD;
                        break;
                    case 19:
                        D = NumberA; A = NumberB; B = NumberC; C = NumberD;
                        break;
                    case 20:
                        D = NumberA; A = NumberB; C = NumberC; B = NumberD;
                        break;
                    case 21:
                        D = NumberA; B = NumberB; A = NumberC; C = NumberD;
                        break;
                    case 22:
                        D = NumberA; B = NumberB; C = NumberC; A = NumberD;
                        break;
                    case 23:
                        D = NumberA; C = NumberB; A = NumberC; B = NumberD;
                        break;
                    case 24:
                        D = NumberA; C = NumberB; B = NumberC; A = NumberD;
                        break;
                }
            }
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
            }
            private void button1_Click(object sender, EventArgs e)
            {
                NumberA=Convert.ToDouble(txt1.Text);
    NumberB=Convert.ToDouble(txt2.Text);
    NumberC=Convert.ToDouble(txt3.Text);
    NumberD=Convert.ToDouble(txt4.Text);
    lblshow.Text="解题中....";
    //1种算法
    for(int i=1;i<=24;i++)
    {
    jisuan24(i);
    if((A+B+C+D)==24)
    lblshow.Text=A + "+" +B + "+"+ C + "+" + D +"=24";
    if((A+B+C-D)==24)
    lblshow.Text=A + "+" +B + "+"+ C + "-" + D +"=24";
    //3
                   if(A * B + C + D==24)
                   lblshow.Text=A + "*" +B + "+"+ C + "+" + D +"=24";
    //4
    if( A * B + C - D==24)
    lblshow.Text=A + "*" +B + "+"+ C + "-" + D +"=24";
    //5
    if( A * B * C + D==24)
    lblshow.Text=A + "*" +B + "*"+ C + "+" + D +"=24";
    //6
    if(A * B * C - D==24)
    lblshow.Text=A + "*" +B + "*"+ C + "-" + D +"=24";
    //7
    if(A * B * C * D==24)
    lblshow.Text=A + "*" +B + "*"+ C + "*" + D +"=24";
    //8
    if(A * B + C * D==24)
    lblshow.Text=A + "*" +B + "+"+ C + "*" + D +"=24";
    //9
                     if(A * B - C * D==24)
    lblshow.Text=A + "*" +B + "-"+ C + "*" + D +"=24";
    //10
    if(A / B + C + D==24)
    lblshow.Text=A + "/" +B + "+"+ C + "+" + D +"=24";
    //11
    if(A * B / C + D==24)
    lblshow.Text=A + "*" +B + "/"+ C + "+" + D +"=24";
    //12
    if(A * B / C - D==24)
    lblshow.Text=A + "*" +B + "/"+ C + "-" + D +"=24";
    //13
    if( A * B * C / D==24)
    lblshow.Text=A + "*" +B + "*"+ C + "/" + D +"=24";
    //14
    if(A * B / C / D==24)
    lblshow.Text=A + "*" +B + "/"+ C + "/" + D +"=24";
    //15
    if( (A + B) * C / D==24)
    lblshow.Text=(A + "+" +B) + "*"+ C + "/" + D +"=24";
    //16
    if((A + B) * C * D==24)
    lblshow.Text=(A + "+" +B) + "*"+ C + "*" + D +"=24";
    //17
    if((A + B) * (C + D)==24)
    lblshow.Text=(A + "+" +B) + "*"+ (C + "+" + D) +"=24";
    //18
    if((A + B) * (C - D)==24)
    lblshow.Text=(A + "+" +B) + "*"+ (C + "-" + D) +"=24";
    //19
    if((A - B) * (C - D)==24)
    lblshow.Text=(A + "-" +B) + "*"+ (C + "-" + D) +"=24";
    //20
    if((A - B) * C / D==24)
    lblshow.Text=(A + "-" +B) + "*"+ (C + "/" + D) +"=24";
    //21
    if((A - B) * C * D==24)
    lblshow.Text=(A + "-" +B) + "*"+ (C + "*" + D) +"=24";
    //22
    if((A + B + C) * D==24)
    lblshow.Text=(A + "+" +B + "+"+ C) + "*" + D +"=24";
    //23
    if((A + B + C) / D==24)
    lblshow.Text=(A + "+" +B + "+"+ C) + "/" + D +"=24";
    //24
    if((A + B - C) * D==24)
    lblshow.Text=(A + "+" +B + "-"+ C) + "*" + D +"=24";
    //25
    if(A * (B + C) + D==24)
    lblshow.Text=A + "*" +(B + "+"+ C) + "+" + D +"=24";
    //26
    if( A * (B - C) + D==24)
    lblshow.Text=A + "*" +(B + "-"+ C) + "+" + D +"=24";
    //27
    if( A * (B + C) - D==24)
    lblshow.Text=A + "*" +(B + "+"+ C) + "-" + D +"=24";
    //28
    if(A * (B - C) - D==24)
    lblshow.Text=A + "*" +(B + "-"+ C) + "-" + D +"=24";
    //29
    if(A + (B + C) / D==24)
    lblshow.Text=A + "+" +(B + "+"+ C) + "/" + D +"=24";
    if((A-B/C)*D==24)
    //30
    lblshow.Text=(A + "-" +B + "/"+ C) + "*" + D +"=24";
        }
     if(lblshow.Text=="解题中....")
    {
    lblshow.Text="此题无解";
    }
    }
            private void button2_Click(object sender, EventArgs e)
            {
                Random rd=new Random();
                int a=rd.Next(1,11);
                int b=rd.Next(1,11);
                int c=rd.Next(1,11);
                int d=rd.Next(1,11);
                //if(a==)
                txt1.Text=a.ToString();
                txt2.Text=b.ToString();
                txt3.Text=c.ToString();
                txt4.Text=d.ToString();
                lblshow.Text="出题完毕,请解答";   
            }
            }
        }


    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多