to lostinet:阿哥教训的是.我详细说一下:就是在命令行下输入四个数字(1到12之间的)然后通过穷举法算出24这个结果来,并输出算法的过程比方说我输入的是:3,3,8,8..输出是8/(3-8/3)..阿哥给个提示!最好能给我源程序看一下...

解决方案 »

  1.   

    能帮一下忙改一下这个程序么?把它改成C#么?
    #include <iostream>
    #include <string>
    #include <cmath>using namespace std;const double PRECISION = 1E-6;
    const int COUNT_OF_NUMBER  = 4;
    const int NUMBER_TO_BE_CAL = 24;double number[COUNT_OF_NUMBER];
    string expression[COUNT_OF_NUMBER];bool Search(int n)
    {
        if (n == 1) {
            if ( fabs(number[0] - NUMBER_TO_BE_CAL) < PRECISION ) {
                cout << expression[0] << endl;
                return true;
            } else {
                return false;
            }
        }    for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                double a, b;
                string expa, expb;            a = number[i];
                b = number[j];
                number[j] = number[n - 1];            expa = expression[i];
                expb = expression[j];
                expression[j] = expression[n - 1];            expression[i] = '(' + expa + '+' + expb + ')';
                number[i] = a + b;
                if ( Search(n - 1) ) return true;
                
                expression[i] = '(' + expa + '-' + expb + ')';
                number[i] = a - b;
                if ( Search(n - 1) ) return true;
                
                expression[i] = '(' + expb + '-' + expa + ')';
                number[i] = b - a;
                if ( Search(n - 1) ) return true;
                                        expression[i] = '(' + expa + '*' + expb + ')';
                number[i] = a * b;
                if ( Search(n - 1) ) return true;            if (b != 0) {
                    expression[i] = '(' + expa + '/' + expb + ')';
                    number[i] = a / b;
                    if ( Search(n - 1) ) return true;
                } 
                if (a != 0) {
                    expression[i] = '(' + expb + '/' + expa + ')';
                    number[i] = b / a;
                    if ( Search(n - 1) ) return true;
                }            number[i] = a;
                number[j] = b;
                expression[i] = expa;
                expression[j] = expb;
            }
        }
        return false;
    }void main()
    {
        for (int i = 0; i < COUNT_OF_NUMBER; i++) {
            char buffer[20];
            int  x;
            cin >> x;
            number[i] = x;
            itoa(x, buffer, 10);
            expression[i] = buffer;
        }    if ( Search(COUNT_OF_NUMBER) ) {
            cout << "Success." << endl;
        } else {
            cout << "Fail." << endl;
        }        
    }
      

  2.   

    哦,是我没有想到。
    I Apologize..
      

  3.   

    to lostinet:可以帮忙该一下程序,使它成为c #么>?
      

  4.   

    偶修改了一下,可以输出1到12这12张纸牌的任意4个的组合,不过因为数据太多,所以程序里面限制了一下,输出100个成功的后,就退出来了。原算法可以修改一下,因为有的组合,可以有多种算法。
    函数就两个:Search和Button1_Click。
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication6{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
    private double PRECISION = 0.000001;
    private int COUNT_OF_NUMBER  = 4;
    private int NUMBER_TO_BE_CAL = 24; private double[] number = new double[4];
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Button button1;
    private string[] expression = new string[4]; private bool Search(int n) {
    if (n == 1){
    if(Math.Abs(number[0] - NUMBER_TO_BE_CAL)<PRECISION){
    // cout << expression[0] << endl;
    return true;

    else{
    return false;
    }
    } for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
    double a, b;
    string expa, expb; a = number[i];
    b = number[j];
    number[j] = number[n - 1]; expa = expression[i];
    expb = expression[j];
    expression[j] = expression[n - 1]; expression[i] = '(' + expa + '+' + expb + ')';
    number[i] = a + b;
    if ( Search(n - 1) ) return true;
                
    expression[i] = '(' + expa + '-' + expb + ')';
    number[i] = a - b;
    if ( Search(n - 1) ) return true;
                
    expression[i] = '(' + expb + '-' + expa + ')';
    number[i] = b - a;
    if ( Search(n - 1) ) return true;
                             expression[i] = '(' + expa + '*' + expb + ')';
    number[i] = a * b;
    if ( Search(n - 1) ) return true; if (b != 0) {
    expression[i] = '(' + expa + '/' + expb + ')';
    number[i] = a / b;
    if ( Search(n - 1) ) return true;

    if (a != 0) {
    expression[i] = '(' + expb + '/' + expa + ')';
    number[i] = b / a;
    if ( Search(n - 1) ) return true;
    } number[i] = a;
    number[j] = b;
    expression[i] = expa;
    expression[j] = expb;
    }
    }
    return false;
    } public Form1(){
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent(); //
    // TODO: Add any constructor code after InitializeComponent call
    //
    } /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing ){
    if( disposing ){
    if (components != null){ 
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent(){
    this.listBox1 = new System.Windows.Forms.ListBox();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // listBox1
    // 
    this.listBox1.Location = new System.Drawing.Point(16, 8);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(552, 264);
    this.listBox1.TabIndex = 0;
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(240, 296);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(584, 341);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.button1,
      this.listBox1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(){ 
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e) {
    int count = 0;
    string tmp; for(int a=1;a<13;a++){
    for(int b=1;b<13;b++){
    for(int c=1;c<13;c++){
    for(int d=1;d<13;d++){
    number[0] = Convert.ToDouble(a);
    number[1] = Convert.ToDouble(b);
    number[2] = Convert.ToDouble(c);
    number[3] = Convert.ToDouble(d); tmp = "";
    for(int i=0;i<4;i++){
    expression[i] = Convert.ToString(number[i]);
    tmp += expression[i];
    tmp += ",";
    }
    tmp = tmp.Substring(0,tmp.Length - 1); if(Search(4)==true){
    count++;
    if(count>100)return;
    listBox1.Items.Add(tmp + ":" + expression[0]);
    }
    }
    }
    }
    }
    }
    }
    }
      

  5.   

    to juqiang:谢谢,如果只需要在界面中输入四个数字,然后算出这四个数字得出24的过程..(比方说输入3,3,8,8就过程(8/(3-8/3)))而不是直接得出所有的结果,那应该怎么做呢?
      

  6.   

    TO juqiang(鞠强) 兄:
      哦,那你能够也发给我过来吗?谢谢!!!
      

  7.   

    TO juqiang(鞠强) 兄:
      哦,那你能够也发给我过来吗?谢谢!!!
     [email protected]
      

  8.   

    [email protected]发给你了!!!
      

  9.   

    TO danspeed:阿哥,程序在哪呢?look一下!?
      

  10.   

    我已经完成了这个程序,但帖子太长发不了,想要程序,请留下email
      

  11.   

    我的E-MAIL:[email protected],
    阿哥,谢谢你!