随便找个源码..
用java改写一下就行了

解决方案 »

  1.   

    最简单的就是四重循环,选出四个数之后,套入事先定好的模版,算出24搞定。
    比如:
    for(A=0; A<09; A++){
      for(B) {
        for(C) {
          for(D) {
            //以下为各种模版
            if((A+B+C+D)==24) {
              print(A,B,C,D);
            } else if((A*B+C+D)==24) {
            } else if(xxx) {
            }
          }
        }
      }
    }
    当然还有更高的AI算法,那就懒得去想了
      

  2.   

    for(int i=0;i<4;i++){   //返回随机数,,,4改为7就是生成彩票6+1的了,我算了几次,,经常中五块的,哈哈,,

    System.out.print((int)(9*Math.random()));


    }
      

  3.   

    不过上面的for(A=0; A<09; A++){
      for(B) {
        for(C) {
          for(D) {
            //以下为各种模版
            if((A+B+C+D)==24) {
              print(A,B,C,D);
            } else if((A*B+C+D)==24) {
            } else if(xxx) {
            }
          }
        }
      }
    }
    这种if-else的判断也太多了吧
      

  4.   

    数据规模大可以搜索来解,考虑适当的剪枝.PS.这里是J2EE版...
      

  5.   

    我找到一个比较好的算法,但是运行的时候出现java.lang.ArrayIndexOutOfBoundsException,应该是数组出现的问题但是我不知道如何修改程序还希望大家帮忙。程序如下
    package tax;
    /*
     * 运算24点的工具
     *
     * Copyleft 2004 戴云杰 xdanger All Rights Reserved.
     * http://www.xdanger.com
     *
     * 使用方式:
     * 键入4个1-10的整数,即可显示所有结果等于24的计算方式
     */import java.io.*;
    import java.lang.*;
    //import java.applet.Applet;
    //import java.awt.*;class Do24{
        // 目标值
        public final short DST_VALUE = ( short ) 24;
        // 输入值列表
        private short[] List = new short[4];
        // 结果列表
        private String[] Result = new String[1024];
        // 结果总数
        private short ResultNumber = 0;
        // 构造函数
        public Do24( short a, short b, short c, short d ){
            this.List[0] = a;
            this.List[1] = b;
            this.List[2] = c;
            this.List[3] = d;
            this.Result = this.countIt();
        }
        // 主函数
        public static void main( String[] args ){
            short a = Short.parseShort( args[0] );
            short b = Short.parseShort( args[1] );
            short c = Short.parseShort( args[2] );
            short d = Short.parseShort( args[3] );
            Do24 o = new Do24( a, b, c, d );
            o.printResult();    }
        // 开始计算
        public String[] countIt(){
            for( short l1 = 0; l1 < 4 ; l1++ ){
                short A;
                A = this.List[l1];
                for( short l2 = 0; l2 < 4 ; l2++){
                    int B = this.List[l2];
                    for( short l3 = 0; l3 < 4 ; l3++){
                        int C = this.List[l3];
                        for( short l4 = 0; l4<4 ; l4++){
                            int D = this.List[l4];
                            if( l1 != l2 && l2 != l3 && l3 != l4 && l4 != l1 && l1 != l3 && l2 != l4 ){
                                if( A + B + C + D == this.DST_VALUE )
                                    this.putResult( A + "+" + B + "+" + C + "+" + D + "=" + this.DST_VALUE );
                                if( A + B + C - D == this.DST_VALUE )
                                    this.putResult( A + "+" + B + "+" + C + "-" + D + "=" + this.DST_VALUE );
                                if( A + B - C - D == this.DST_VALUE )
                                    this.putResult( A + "+" + B + "-" + C + "-" + D + "=" + this.DST_VALUE );
                                if( A - B - C - D == this.DST_VALUE )
                                    this.putResult( A + "-" + B + "-" + C + "-" + D + "=" + this.DST_VALUE );
                                if( A * B * C * D == this.DST_VALUE )
                                    this.putResult( A + "*" + B + "*" + C + "*" + D + "=" + this.DST_VALUE );
                                if( A * B * C - D == this.DST_VALUE )
                                    this.putResult( A + "*" + B + "*" + C + "-" + D + "=" + this.DST_VALUE );
                                if( A * B - C - D == this.DST_VALUE )
                                    this.putResult( A + "*" + B + "-" + C + "-" + D + "=" + this.DST_VALUE );
                                if( A * B - C * D == this.DST_VALUE )
                                    this.putResult( A + "*" + B + "-" + C + "*" + D + "=" + this.DST_VALUE );
                                if( A * B * C + D == this.DST_VALUE )
                                    this.putResult( A + "*" + B + "*" + C + "+" + D + "=" + this.DST_VALUE );
                                if( A * B + C + D == this.DST_VALUE )
                                    this.putResult( A + "*" + B + "+" + C + "+" + D + "=" + this.DST_VALUE );
                                if( A * B + C * D == this.DST_VALUE )
                                    this.putResult( A + "*" + B + "+" + C + "*" + D + "=" + this.DST_VALUE );
                                if( A * B + C - D == this.DST_VALUE )
                                    this.putResult( A + "*" + B + "+" + C + "-" + D + "=" + this.DST_VALUE );
                                if( A - B * C * D == this.DST_VALUE )
                                    this.putResult( A + "-" + B + "*" + C + "*" + D + "=" + this.DST_VALUE );
                                if( A + B - C * D == this.DST_VALUE )
                                    this.putResult( A + "+" + B + "-" + C + "*" + D + "=" + this.DST_VALUE );
                                if( A * B * (C + D) == this.DST_VALUE )
                                    this.putResult( A + "*" + B + "*(" + C + "+" + D + ")=" + this.DST_VALUE );
                                if( A * B * (C - D) == this.DST_VALUE )
                                    this.putResult( A + "*" + B + "*(" + C + "-" + D + ")=" + this.DST_VALUE );
                                if( A * (B + C + D) == this.DST_VALUE )
                                    this.putResult( A + "*(" + B + "+" + C + "+" + D + ")=" + this.DST_VALUE );
                                if( A * (B + C - D) == this.DST_VALUE )
                                    this.putResult( A + "*(" + B + "+" + C + "-" + D + ")=" + this.DST_VALUE );
                                if( A * (B - C - D) == this.DST_VALUE )
                                    this.putResult( A + "*(" + B + "-" + C + "-" + D + ")=" + this.DST_VALUE );
                                if( A - B * (C + D) == this.DST_VALUE )
                                    this.putResult( A + "-" + B + "*(" + C + "+" + D + ")=" + this.DST_VALUE );
                                if( A - B * (C - D) == this.DST_VALUE )
                                    this.putResult( A + "-" + B + "*(" + C + "-" + D + ")=" + this.DST_VALUE );
                                if( A * (B + C) - D == this.DST_VALUE )
                                    this.putResult( A + "*(" + B + "+" + C + ")-" + D + "=" + this.DST_VALUE );
                                if( A * (B + C) + D == this.DST_VALUE )
                                    this.putResult( A + "*(" + B + "+" + C + ")+" + D + "=" + this.DST_VALUE );
                                if( A * (B - C) - D == this.DST_VALUE )
                                    this.putResult( A + "*(" + B + "-" + C + ")-" + D + "=" + this.DST_VALUE );
                                if( A * (B - C) + D == this.DST_VALUE )
                                    this.putResult( A + "*(" + B + "-" + C + ")+" + D + "=" + this.DST_VALUE );
                                if( (A + B) * (C - D) == this.DST_VALUE )
                                    this.putResult( "(" + A + "+" + B + ")*(" + C + "-" + D + ")=" + this.DST_VALUE );
                                if( (A + B) * (C + D) == this.DST_VALUE )
                                    this.putResult( "(" + A + "+" + B + ")*(" + C + "+" + D + ")=" + this.DST_VALUE );
                                if( (A - B) * (C - D) == this.DST_VALUE )
                                    this.putResult( "(" + A + "-" + B + ")*(" + C + "-" + D + ")=" + this.DST_VALUE );
      

  6.   

    if( B != 0 && A > B && A % B == 0 ){
                                    if( A / B + C + D == this.DST_VALUE )
                                        this.putResult( A + "/" + B + "+" + C + "+" + D + "=" + this.DST_VALUE );
                                    if( A / B + C - D == this.DST_VALUE )
                                        this.putResult( A + "/" + B + "+" + C + "-" + D + "=" + this.DST_VALUE );
                                     if( A / B - C - D == this.DST_VALUE )
                                        this.putResult( A + "/" + B + "-" + C + "-" + D + "=" + this.DST_VALUE );
                                    if( A / B * C + D == this.DST_VALUE )
                                        this.putResult( A + "/" + B + "*" + C + "+" + D + "=" + this.DST_VALUE );
                                    if( A / B + C * D == this.DST_VALUE )
                                        this.putResult( A + "/" + B + "+" + C + "*" + D + "=" + this.DST_VALUE );
                                    if( A / B - C * D == this.DST_VALUE )
                                        this.putResult( A + "/" + B + "-" + C + "*" + D + "=" + this.DST_VALUE );
                                    if( A / B * C - D == this.DST_VALUE )
                                        this.putResult( A + "/" + B + "*" + C + "-" + D + "=" + this.DST_VALUE );
                                    if( A / B * C * D == this.DST_VALUE )
                                        this.putResult( A + "/" + B + "*" + C + "*" + D + "=" + this.DST_VALUE );
                                    if( C * D - A / B == this.DST_VALUE )
                                        this.putResult( C + "*" + D + "-" + A + "/" + B + "=" + this.DST_VALUE );
                                    if( C - D - A / B == this.DST_VALUE )
                                        this.putResult( C + "-" + D + "-" + A + "/" + B + "=" + this.DST_VALUE );
                                    if( C + D - A / B == this.DST_VALUE )
                                        this.putResult( C + "+" + D + "-" + A + "/" + B + "=" + this.DST_VALUE );
                                    if( C + D + A / B == this.DST_VALUE )
                                        this.putResult( C + "+" + D + "+" + A + "/" + B + "=" + this.DST_VALUE );
                                    if( C * ( D - A / B) == this.DST_VALUE )
                                        this.putResult( C + "*(" + D + "-" + A + "/" + B + ")=" + this.DST_VALUE );
                                    if( C * ( D + A / B) == this.DST_VALUE )
                                        this.putResult( C + "*(" + D + "+" + A + "/" + B + ")=" + this.DST_VALUE );
                                    if( C * ( A / B - D) == this.DST_VALUE )
                                        this.putResult( C + "*(" + A + "/" + B + "-" + D + ")=" + this.DST_VALUE );
                                    if( D != 0 && C > D && C % D == 0 ){
                                        if( (A / B) + (C / D) == this.DST_VALUE )
                                            this.putResult( "(" + A + "/" + B + ")+(" + C + "/" + D + "=" + this.DST_VALUE );
                                        if( (A / B) - (C / D) == this.DST_VALUE )
                                            this.putResult( "(" + A + "/" + B + ")-(" + C + "/" + D + "=" + this.DST_VALUE );
                                        if( (A / B) * (C / D) == this.DST_VALUE )
                                            this.putResult( "(" + A + "/" + B + ")*(" + C + "/" + D + "=" + this.DST_VALUE );
                                        if( A / B / D + C == this.DST_VALUE )
                                            this.putResult( A + "/" + B + "/" + D + "+" + C + "=" + this.DST_VALUE );
                                        if( A / B / D - C == this.DST_VALUE )
                                            this.putResult( A + "/" + B + "/" + D + "-" + C + "=" + this.DST_VALUE );
                                        if( A / B / D * C == this.DST_VALUE )
                                            this.putResult( A + "/" + B + "/" + D + "*" + C + "=" + this.DST_VALUE );
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return this.Result;
           
        }
        
        // 打印结果
        public void printResult(){
            for( short i = 0; i < this.getResultNumber(); i++ )
                System.out.print( this.Result[i] + '\n' );
        }
        // 放入结果列表
        public void putResult( String result ){
            this.Result[this.getResultNumber()] = result;
            this.addResultNumber();
        }
        // 取得结果数目
        public short getResultNumber(){
            return this.ResultNumber;
        }
        // 设置结果数目
        private void setResultNumber( short number ){
            this.ResultNumber = number;
        }
        // 增加1个结果数目
        private void addResultNumber(){
            this.ResultNumber ++;
        }
      
    }
      

  7.   

    很帅气的一个程序啊这个程序要求从命令行直接接受四个待算的数,java.lang.ArrayIndexOutOfBoundsException出现是由于你没有在运行时输满这四个数。
    java Do24 1 2 3 4
    看吧,有结果了但是程序是有问题的,像“1 3 4 6”和“3 3 8 8”这些比较经典的要用到分数的都没有结果,应该要改进啊。楼主还要程序么,我编了一个,回复就给你试试,正确输出所有结果应该没问题。
      

  8.   

    云那,我给你我写的一个用c++的吧,很好改成java的。
    #include<iostream>
    #include<string>
    #include<vector>
    #include<set>
    using namespace std;
    set<string>vs;
    //author: JLU_faen of software
    string gc(int n)
    {
    string s;
    bool tag=false;
    if(n<0){tag=true;n=-n;}
    if(n==0){s="0";return s;}
    while(n)
    {
        s=char(n%10+'0')+s;
        n/=10;
        }
        if(tag)s='-'+s;    
    return s;
    }
    void g(vector<int> vi,string s)
    {
    if(vi.size()==1)
    if(vi[0]==24)
    {
    vs.insert(s);
    return;
    }
    else return;
    for(int i=0;i<vi.size();i++)
    for(int j=i+1;j<vi.size();j++)
    {
        //if(j==i)continue;
    vector<int>vit;
    string snext;
    for(int k=0;k<vi.size();k++)
    if(k!=i&&k!=j)
    vit.push_back(vi[k]);

    vit.push_back(vi[i]+vi[j]);
    snext=s+"   "+gc(vi[i])+"+"+gc(vi[j])+"="+gc(vi[i]+vi[j]);
    g(vit,snext);
    snext="";
    vit.pop_back(); vit.push_back(vi[i]-vi[j]);
    snext=s+"   "+gc(vi[i])+"-"+gc(vi[j])+"="+gc(vi[i]-vi[j]);
    g(vit,snext);
    vit.pop_back();
    snext=""; vit.push_back(vi[i]*vi[j]);
    snext=s+"   "+gc(vi[i])+"*"+gc(vi[j])+"="+gc(vi[i]*vi[j]);
    g(vit,snext);
    vit.pop_back();
    snext=""; if(vi[j]&&vi[i]%vi[j]==0)
    {
    vit.push_back(vi[i]/vi[j]);
    snext=s+"   "+gc(vi[i])+"/"+gc(vi[j])+"="+gc(vi[i]/vi[j]);
    g(vit,snext);
    vit.pop_back();
    snext="";
    }
    if(vi[i]&&vi[j]%vi[i]==0)
    {
    vit.push_back(vi[j]/vi[i]);
    snext=s+"   "+gc(vi[j])+"/"+gc(vi[i])+"="+gc(vi[j]/vi[i]);
    g(vit,snext);
    vit.pop_back();
    snext="";
    }
    }
    }
    int main()
    {
    int a,b,c,d;
    cout<<"please input four numbers:\n";
    cin>>a>>b>>c>>d;
    vector<int>vi;
    vi.push_back(a);
    vi.push_back(b);
    vi.push_back(c);
    vi.push_back(d);
    string s;
    g(vi,s);
    if(vs.size())
    {
    set<string>::iterator p=vs.begin();
    for(;p!=vs.end();p++)
    cout<<*p<<endl;
    }
    else
    cout<<"sorry the four numbers can't be done"<<endl;
    int aa;
    cin>>aa;
    return 0;
    }
      

  9.   

    public class Test24Point{
        public static void main(String[] args){
         int index = 0 ;
         int temp = 0 ;
         int totalSuc = 0 ;
        
         int numb[] = new int[4];//the first four numbers
         double num[][] = new double[36][3];//three numbers after calculating
         double total[] = new double[6];//the number after three steps of calculating
        
        
        
         double p[][] = new double[6][8];
         double q[][] = new double[3][7];
        
        
        
         //System.out.println(2465%108);
         //System.out.println(2465/108);
        
         System.out.println("\"a--b\"means\"b-a\"");
         System.out.println("\"a//b\"means\"b/a\"\n");
    for(int i = 0 ; i < 4 ; i ++){
    numb[i] = Integer.parseInt(args[i]);
    }
        
           for(int i = 0; i < 3; i ++)//Get two of the four to calculate and then store the new number into the array p;
           for(int j = i + 1; j < 4 ; j ++,temp ++){
           p[temp][0] = numb[i] + numb[j]; 
           p[temp][1] = numb[i] - numb[j];
           p[temp][2] = numb[j] - numb[i];
           p[temp][3] = numb[i] * numb[j];
           if(numb[j] != 0)
           p[temp][4] = numb[i] / (double)numb[j];
           else
           p[temp][4] = 10000;
           if(numb[i] != 0)
           p[temp][5] = numb[j] / (double)numb[i];
           else
           p[temp][5] = 10000;
          
          
         
          switch(temp){
          case 0:p[temp][6] = numb[2]; p[temp][7] = numb[3];break;
          case 1:p[temp][6] = numb[1]; p[temp][7] = numb[3];break;
          case 2:p[temp][6] = numb[1]; p[temp][7] = numb[2];break;
          case 3:p[temp][6] = numb[0]; p[temp][7] = numb[3];break;
          case 4:p[temp][6] = numb[0]; p[temp][7] = numb[2];break;
          case 5:p[temp][6] = numb[0]; p[temp][7] = numb[1];
          }
         
          }
         
    for(int k = 0,tem = 0; k < 6; k ++)//Get the possible three numbers and store into the array num[36][3] for calculating .
    for(int l = 0; l < 6; l ++,tem ++){
    num[tem][0] = p[k][l] ;
    num[tem][1] = p[k][6] ;
    num[tem][2] = p[k][7] ;


    for(int t = 2,m = 0, n = 0,te = 0; t >= 0; t --,te ++){//Get two of the three to calculate and then store the new number into the array q; 
       m = (t + 1)%3;
       n = (t + 2)%3;
       q[te][6] = num[tem][t];
      
       q[te][0] = num[tem][m] + num[tem][n];
       q[te][1] = num[tem][m] - num[tem][n];
       q[te][2] = num[tem][n] - num[tem][m];
       q[te][3] = num[tem][m] * num[tem][n];
       if(num[tem][n] != 0)
       q[te][4] = num[tem][m] / (double)num[tem][n];
       else
       q[te][4] = 10000 ;
       if(num[tem][m] != 0)
       q[te][5] = num[tem][n] / (double)num[tem][m];
       else
       q[te][5] = 10000 ;
       }
        
      

  10.   

    for(int u = 0; u < 3; u ++)
         for(int v = 0; v < 6; v ++){
         if(u == 2){//We must insure that the old value is in the left ,so the result string can be appended rightly.
         total[0] = q[u][6] + q[u][v];
         total[1] = q[u][6] - q[u][v];
         total[2] = q[u][v] - q[u][6];
         total[3] = q[u][v] * q[u][6];
         if(q[u][6] != 0)
         total[4] = q[u][6] / (double)q[u][v];
         else
         total[4] = 10000;
         if(q[u][v] != 0)
         total[5] = q[u][v] / (double)q[u][6];
         else
         total[5] = 10000;
         }
         else{
         total[0] = q[u][v] + q[u][6];
         total[1] = q[u][v] - q[u][6];
         total[2] = q[u][6] - q[u][v];
         total[3] = q[u][v] * q[u][6];
         if(q[u][6] != 0)
         total[4] = q[u][v] / (double)q[u][6];
         else
         total[4] = 10000;
         if(q[u][v] != 0)
         total[5] = q[u][6] / (double)q[u][v];
         else
         total[5] = 10000;
         }
        
         for(int s = 0 ; s < 6 ; s ++){
         if(total[s]>23.9999&&total[s]<24.0001){
         //System.out.println("24!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
         totalSuc ++ ;
         //print the expression
    char x[] = new char[3];
    int n0 = index ;
    String expre = "" ;
    String expre1 = "" ;
    int n1 = index/108;//the first composition and its operator.
    int n2 = index%108/6;//the second composition and its operator.
    int n3 = index%6;//the last operator.

    if(index == 3321){System.out.println(q[2][1] + " " +q[2][6]);}

    switch(n1){
         
          case 0:expre += "((a+b)";x[1]='c';x[2]='d';break;
          case 1:expre += "((a-b)";x[1]='c';x[2]='d';break;
          case 2:expre += "((b-a)";x[1]='c';x[2]='d';break;
          case 3:expre += "((a*b)";x[1]='c';x[2]='d';break;
          case 4:expre += "((a/b)";x[1]='c';x[2]='d';break;
          case 5:expre += "((b/a)";x[1]='c';x[2]='d';break;
          case 6:expre += "((a+c)";x[1]='b';x[2]='d';break;
          case 7:expre += "((a-c)";x[1]='b';x[2]='d';break;
          case 8:expre += "((c-a)";x[1]='b';x[2]='d';break;
          case 9:expre += "((a*c)";x[1]='b';x[2]='d';break;
          case 10:expre += "((a/c)";x[1]='b';x[2]='d';break;
          case 11:expre += "((c/a)";x[1]='b';x[2]='d';break;
          case 12:expre += "((a+d)";x[1]='b';x[2]='c';break;
          case 13:expre += "((a-d)";x[1]='b';x[2]='c';break;
          case 14:expre += "((d-a)";x[1]='b';x[2]='c';break;
          case 15:expre += "((a*d)";x[1]='b';x[2]='c';break;
          case 16:expre += "((a/d)";x[1]='b';x[2]='c';break;
          case 17:expre += "((d/a)";x[1]='b';x[2]='c';break;
          case 18:expre += "((b+c)";x[1]='a';x[2]='d';break;
          case 19:expre += "((b-c)";x[1]='a';x[2]='d';break;
          case 20:expre += "((c-b)";x[1]='a';x[2]='d';break;
          case 21:expre += "((b*c)";x[1]='a';x[2]='d';break;
          case 22:expre += "((b/c)";x[1]='a';x[2]='d';break;
          case 23:expre += "((c/b)";x[1]='a';x[2]='d';break;
          case 24:expre += "((b+d)";x[1]='a';x[2]='c';break;
          case 25:expre += "((b-d)";x[1]='a';x[2]='c';break;
          case 26:expre += "((d-b)";x[1]='a';x[2]='c';break;
          case 27:expre += "((b*d)";x[1]='a';x[2]='c';break;
          case 28:expre += "((b/d)";x[1]='a';x[2]='c';break;
          case 29:expre += "((d/b)";x[1]='a';x[2]='c';break;
          case 30:expre += "((c+d)";x[1]='a';x[2]='b';break;
          case 31:expre += "((c-d)";x[1]='a';x[2]='b';break;
          case 32:expre += "((d-c)";x[1]='a';x[2]='b';break;
          case 33:expre += "((c*d)";x[1]='a';x[2]='b';break;
          case 34:expre += "((c/d)";x[1]='a';x[2]='b';break;
          case 35:expre += "((d/c)";x[1]='a';x[2]='b';
          }
         
          switch(n2){
         
          case 0:expre += "+" +x[1] +")";x[1]=x[2];break;//x[0] and x[1].
          case 1:expre += "-" +x[1] +")";x[1]=x[2];break;
          case 2:expre += "--" +x[1] +")";x[1]=x[2];break;
          case 3:expre += "*" +x[1] +")";x[1]=x[2];break;
          case 4:expre += "/" +x[1] +")";x[1]=x[2];break;
          case 5:expre += "//" +x[1] +")";x[1]=x[2];break;
         
          case 6:expre += "+" +x[2] +")";x[1]=x[1];break;//x[2] and x[0].
          case 7:expre += "--" +x[2] +")";x[1]=x[1];break;
          case 8:expre += "-" +x[2] +")";x[1]=x[1];break;
          case 9:expre += "*" +x[2] +")";x[1]=x[1];break;
          case 10:expre += "//" +x[2] +")";x[1]=x[1];break;
          case 11:expre += "/" +x[2] +")";x[1]=x[1];break;
         
          case 12:expre1 += x[1] + "+" + x[2] +"))";x[1]='(';break;//x[1] and x[2].
          case 13:expre1 += x[1] + "-" + x[2] +"))";x[1]='(';break;
          case 14:expre1 += x[1] + "--" + x[2] +"))";x[1]='(';break;
          case 15:expre1 += x[1] + "*" + x[2] +"))";x[1]='(';break;
          case 16:expre1 += x[1] + "/" + x[2] +"))";x[1]='(';break;
          case 17:expre1 += x[1] + "//" + x[2] +"))";x[1]='(';
          }
         
          switch(n3){
         
          case 0:expre += "+" +x[1] + expre1;break;
          case 1:expre += "-" +x[1] + expre1;break;
          case 2:expre += "--" +x[1] + expre1;break;
          case 3:expre += "*" +x[1] + expre1;break;
          case 4:expre += "/" +x[1] + expre1;break;
          case 5:expre += "//" +x[1] + expre1;
          }
          expre = expre.replace('a',(char)(numb[0] + 48));
          expre = expre.replace('b',(char)(numb[1] + 48));
          expre = expre.replace('c',(char)(numb[2] + 48));
          expre = expre.replace('d',(char)(numb[3] + 48));
         
          System.out.println(expre+"  ");
         }
         //System.out.println("total : " + total[s] + " index :" +"\n\n");
        
         
           index ++ ;
    }

    }
         
         }System.out.println("The number of total successful expressions is : " + totalSuc);
    }
        
    }
      

  11.   

    specialsoldier(雪地撒野) 的不好用
    如:2 6 9 2
      

  12.   

    to 雪地撒野,你的程序也很好不过我运行的结果怎么只有
    "a--b"means"b-a"
    "a//b"means"b/a"
    ???
    觉得好困惑呀
      

  13.   

    这个程序要求从命令行直接接受四个待算的数,java.lang.ArrayIndexOutOfBoundsException出现是由于你没有在运行时输满这四个数。
    java Test24Point 1 2 3 4
    看吧,有结果了大家好好试嘛,2 6 9 2 是可以的哦:
    C:\金山词霸2005专业版\KPW2005\Common\kingsoft\Extract\Sound\hehe\t>java Test24Point 2 6 9 2
    "a--b"means"b-a"
    "a//b"means"b/a"((6/2)+9)*2
    ((2*9)-6)*2
    ((6/2)+9)*2
    ((9*2)-6)*2
    The number of total successful expressions is : 4
      

  14.   

    一群大牛:一个比一个代码写得长,给大家个算法,本人觉得可以,不过可能会慢点
    1,想办法把 +,-,*,/ 每次出现的时候随机化!
    2,随机抽出4个数字进行运算,
    3,跳出结果等于24的以下是部分代码
    public int add(int a, int b){
      return a+b;
    }
    public int sub(int a, int b){
      return a-b;
    }
    public int mult(int a, int b){
      return a*b;
    }
    public int div(int a, int b){
      return a/b;
    }public static void main(String [] args){
    int result = (int)Math.random()*9);
    ArrayList<String> resultlist=new ArrayList()<String>;
    for(int i=0; i<4;i++){
    String op=null;
      switch((int) Math.random*4){
      case:1 result = add(result,(int)Math.random()*9);op="+";
      case:2 result = sub(result,(int)Math.random()*9);op="-";
      case:3 result = mult(result,(int)Math.random()*9);op="*";
      case:4 result = div(result,(int)Math.random()*9);op="/";
      }
      resultlist.add(result.toString());
      resultlist.add(op.toString());
    }
    if(result=24){
    resultlist.add("ok");
    }
    for(int j=0;j<result.length;j++){
    String tmp=result.get(j);
    if(tmp=="ok"){
    for(int i=0;i<8;i++){
       System.out.println(result.get(j-(8-i));
        }
      }
    }括号的情况可以依次类推;我可没有上机调试过啊,错了不要怪我!!!
      

  15.   

    呵呵,xdfbb(micwolf) :
    你的想法是蛮有灵性的,但你的程序是有问题的:1,第一次确实是可以任意两个值做四则运算,但第二次并不都是用上次算的结果和一新值做运算,也可能是另外两个新值先做运算,然后两个运算结果做四则,所以对于这个等式(2+2)×(3+3),我想你的程序是至死也算不出的。
    2,你只能输出一个等式,这样适应面会比较窄,需求一变,就要做大手术了。我的程序确实比较混乱,也确实比较长(若不考虑输出,可以少100行呵呵)但应该没有逻辑方面的错误,恩,不过还得改进。我觉得大家应该互相鼓励,什么“一群大牛”之类的,那是日本人的秉性,那叫一欠cao。
      

  16.   

    specialsoldier(雪地撒野) 先生的代码是正确的,不过不好懂。
    收藏大家去看看这个问题,解释清楚的另外加分,谢谢:http://community.csdn.net/Expert/topic/3942/3942532.xml?temp=.8107569
      

  17.   

    to 发恩,我只是懂一些c语言所以你的程序我就不怎么会改成java,真是不好意思
      

  18.   

    to 雪地撒野 我已经知道了,我为什么出不来结果了,我用的调试工具是
    eclipse,在dos下面调试应该可以出来结果的,又学到东西了
      

  19.   

    改造了一下雪地撒野的程序:
    import java.util.ArrayList;public class Test24Point1{
    final int TOTAL = 24;
    final int NUMBER = 4;
    final int OPNUMBER = 6;

    public int numb[] = new int[NUMBER];//the first four numbers
    ArrayList result = new ArrayList();

    double p[][] = new double[NUMBER * (NUMBER - 1) / 2][OPNUMBER + NUMBER - 2];
    double q[][] = new double[NUMBER - 1][OPNUMBER + (NUMBER - 1) - 2];

    int index = 0;

    public Test24Point1(int a, int b, int c, int d){
    numb[0] = a;
    numb[1] = b;
    numb[2] = c;
    numb[3] = d;
    }

    private void calculate1(){
    int temp = 0 ;

           for(int i = 0; i < NUMBER - 1; i++)//Get two of the four to calculate and then store the new number into the array p;
           for(int j = i + 1; j < NUMBER ; j++, temp++){
           p[temp][0] = numb[i] + numb[j]; 
           p[temp][1] = numb[i] - numb[j];
           p[temp][2] = numb[j] - numb[i];
           p[temp][3] = numb[i] * numb[j];
           if(numb[j] != 0)
           p[temp][4] = numb[i] / (double)numb[j];
           else
           p[temp][4] = 10000;
           if(numb[i] != 0)
           p[temp][5] = numb[j] / (double)numb[i];
           else
           p[temp][5] = 10000;

          switch(temp){
          case 0:p[temp][6] = numb[2]; p[temp][7] = numb[3];break;
          case 1:p[temp][6] = numb[1]; p[temp][7] = numb[3];break;
          case 2:p[temp][6] = numb[1]; p[temp][7] = numb[2];break;
          case 3:p[temp][6] = numb[0]; p[temp][7] = numb[3];break;
          case 4:p[temp][6] = numb[0]; p[temp][7] = numb[2];break;
          case 5:p[temp][6] = numb[0]; p[temp][7] = numb[1];
          }
         
          }
    }

    private void calculate2(){
    double num[][] = new double[OPNUMBER * NUMBER * (NUMBER - 1) / 2][NUMBER - 1];//three numbers after calculating

    for(int k = 0,tem = 0; k < NUMBER * (NUMBER - 1) / 2; k++)//Get the possible three numbers and store into the array num[][] for calculating .
    for(int l = 0; l < OPNUMBER; l++, tem++){
    num[tem][0] = p[k][l] ;
    num[tem][1] = p[k][6] ;
    num[tem][2] = p[k][7] ;


    for(int t = 2, m = 0, n = 0, te = 0; t >= 0; t--, te++){//Get two of the three to calculate and then store the new number into the array q; 
       m = (t + 1)%3;
       n = (t + 2)%3;
       q[te][6] = num[tem][t];
      
       q[te][0] = num[tem][m] + num[tem][n];
       q[te][1] = num[tem][m] - num[tem][n];
       q[te][2] = num[tem][n] - num[tem][m];
       q[te][3] = num[tem][m] * num[tem][n];
       if(num[tem][n] != 0)
       q[te][4] = num[tem][m] / (double)num[tem][n];
       else
       q[te][4] = 10000 ;
       if(num[tem][m] != 0)
       q[te][5] = num[tem][n] / (double)num[tem][m];
       else
       q[te][5] = 10000 ;
       }
    calculate3();
         }
    }

      

  20.   

    private void calculate3(){
    double total[] = new double[OPNUMBER];//the number after three steps of calculating

    for(int u = 0; u < 3; u++)
         for(int v = 0; v < OPNUMBER; v++){
         if(u == 2){//We must insure that the old value is at the left ,so the result string can be appended rightly.
         total[0] = q[u][6] + q[u][v];
         total[1] = q[u][6] - q[u][v];
         total[2] = q[u][v] - q[u][6];
         total[3] = q[u][v] * q[u][6];
         if(q[u][6] != 0)
         total[4] = q[u][6] / (double)q[u][v];
         else
         total[4] = 10000;
         if(q[u][v] != 0)
         total[5] = q[u][v] / (double)q[u][6];
         else
         total[5] = 10000;
         }
         else{
         total[0] = q[u][v] + q[u][6];
         total[1] = q[u][v] - q[u][6];
         total[2] = q[u][6] - q[u][v];
         total[3] = q[u][v] * q[u][6];
         if(q[u][6] != 0)
         total[4] = q[u][v] / (double)q[u][6];
         else
         total[4] = 10000;
         if(q[u][v] != 0)
         total[5] = q[u][6] / (double)q[u][v];
         else
         total[5] = 10000;
         }
        
         for(int s = 0; s < OPNUMBER; s++){
         if(total[s] > TOTAL - .0001 && total[s] < TOTAL + .0001){
         //print the expression
    char x[] = new char[3];
    int n0 = index ;
    String expre = "" ;
    String expre1 = "" ;
    int n1 = index / (OPNUMBER * NUMBER * (NUMBER - 1) / 2 * (NUMBER - 1));//the first composition and its operator.
    int n2 = index % (OPNUMBER * NUMBER * (NUMBER - 1) / 2 * (NUMBER - 1)) / OPNUMBER;//the second composition and its operator.
    int n3 = index % OPNUMBER;//the last operator. switch(n1){
          case 0:expre += "((a+b)";x[1]='c';x[2]='d';break;
          case 1:expre += "((a-b)";x[1]='c';x[2]='d';break;
          case 2:expre += "((b-a)";x[1]='c';x[2]='d';break;
          case 3:expre += "((a*b)";x[1]='c';x[2]='d';break;
          case 4:expre += "((a/b)";x[1]='c';x[2]='d';break;
          case 5:expre += "((b/a)";x[1]='c';x[2]='d';break;
          case 6:expre += "((a+c)";x[1]='b';x[2]='d';break;
          case 7:expre += "((a-c)";x[1]='b';x[2]='d';break;
          case 8:expre += "((c-a)";x[1]='b';x[2]='d';break;
          case 9:expre += "((a*c)";x[1]='b';x[2]='d';break;
          case 10:expre += "((a/c)";x[1]='b';x[2]='d';break;
          case 11:expre += "((c/a)";x[1]='b';x[2]='d';break;
          case 12:expre += "((a+d)";x[1]='b';x[2]='c';break;
          case 13:expre += "((a-d)";x[1]='b';x[2]='c';break;
          case 14:expre += "((d-a)";x[1]='b';x[2]='c';break;
          case 15:expre += "((a*d)";x[1]='b';x[2]='c';break;
          case 16:expre += "((a/d)";x[1]='b';x[2]='c';break;
          case 17:expre += "((d/a)";x[1]='b';x[2]='c';break;
          case 18:expre += "((b+c)";x[1]='a';x[2]='d';break;
          case 19:expre += "((b-c)";x[1]='a';x[2]='d';break;
          case 20:expre += "((c-b)";x[1]='a';x[2]='d';break;
          case 21:expre += "((b*c)";x[1]='a';x[2]='d';break;
          case 22:expre += "((b/c)";x[1]='a';x[2]='d';break;
          case 23:expre += "((c/b)";x[1]='a';x[2]='d';break;
          case 24:expre += "((b+d)";x[1]='a';x[2]='c';break;
          case 25:expre += "((b-d)";x[1]='a';x[2]='c';break;
          case 26:expre += "((d-b)";x[1]='a';x[2]='c';break;
          case 27:expre += "((b*d)";x[1]='a';x[2]='c';break;
          case 28:expre += "((b/d)";x[1]='a';x[2]='c';break;
          case 29:expre += "((d/b)";x[1]='a';x[2]='c';break;
          case 30:expre += "((c+d)";x[1]='a';x[2]='b';break;
          case 31:expre += "((c-d)";x[1]='a';x[2]='b';break;
          case 32:expre += "((d-c)";x[1]='a';x[2]='b';break;
          case 33:expre += "((c*d)";x[1]='a';x[2]='b';break;
          case 34:expre += "((c/d)";x[1]='a';x[2]='b';break;
          case 35:expre += "((d/c)";x[1]='a';x[2]='b';
          }
         
          switch(n2){
         
          case 0:expre += "+" +x[1] +")";x[1]=x[2];break;//x[0] and x[1].
          case 1:expre += "-" +x[1] +")";x[1]=x[2];break;
          case 2:expre = convertMinus(expre, x[1]);x[1]=x[2];break;
          case 3:expre += "*" +x[1] +")";x[1]=x[2];break;
          case 4:expre += "/" +x[1] +")";x[1]=x[2];break;
          case 5:expre = convertDivide(expre, x[1]);x[1]=x[2];break;
         
          case 6:expre += "+" +x[2] +")";x[1]=x[1];break;//x[2] and x[0].
          case 7:expre = convertMinus(expre, x[2]);x[1]=x[1];break;
          case 8:expre += "-" +x[2] +")";x[1]=x[1];break;
          case 9:expre += "*" +x[2] +")";x[1]=x[1];break;
          case 10:expre = convertDivide(expre, x[2]);x[1]=x[1];break;
          case 11:expre += "/" +x[2] +")";x[1]=x[1];break;
         
          case 12:expre1 += x[1] + "+" + x[2] +"))";x[1]='(';break;//x[1] and x[2].
          case 13:expre1 += x[1] + "-" + x[2] +"))";x[1]='(';break;
          case 14:expre1 += x[2] + "-" + x[1] +"))";x[1]='(';break;
          case 15:expre1 += x[1] + "*" + x[2] +"))";x[1]='(';break;
          case 16:expre1 += x[1] + "/" + x[2] +"))";x[1]='(';break;
          case 17:expre1 += x[2] + "/" + x[1] +"))";x[1]='(';
          }
         
          switch(n3){
          case 0:expre += "+" +x[1] + expre1;break;
          case 1:expre += "-" +x[1] + expre1;break;
          case 2:expre = convertMinus(expre, x[1], expre1);break;
          case 3:expre += "*" +x[1] + expre1;break;
          case 4:expre += "/" +x[1] + expre1;break;
          case 5:expre = convertDivide(expre, x[1], expre1);
          }
          expre = expre.replace('a',(char)(numb[0] + 48));
          expre = expre.replace('b',(char)(numb[1] + 48));
          expre = expre.replace('c',(char)(numb[2] + 48));
          expre = expre.replace('d',(char)(numb[3] + 48));
         
          if(!result.contains(expre)){
          result.add(expre);
          }
         }
           index++ ;
    }
    }
    }

      

  21.   

    private String convertMinus(String exp, char x){
    exp = "(" + x + "-" + exp.substring(1) + ")";
      return exp;
    }

    private String convertDivide(String exp, char x){
    exp = "(" + x + "/" + exp.substring(1) + ")";
      return exp;
    } private String convertMinus(String exp, char x, String exp1){
    int l = exp1.length();
    if (l ==0){
    exp = "(" + x + "-" + exp + ")";
    }
    else{
    exp = "(" + x + exp1.substring(0, l -1 ) + "-" + exp.substring(1) + ")";
    }
      return exp;
    }

    private String convertDivide(String exp, char x, String exp1){
    int l = exp1.length();
    if (l ==0){
    exp = "(" + x + "/" + exp + ")";
    }
    else{
    exp = "(" + x + exp1.substring(0, l -1 ) + "/" + exp.substring(1) + ")";
    }
      return exp;
    }

    private void printResult(){
    System.out.println("\r\n" + numb[0] + " " + numb[1] + " " + numb[2] + " " + numb[3] + ":");
    System.out.println("The number of total successful expressions is : " + result.size());
            for( short i = 0; i < result.size(); i++ )
                System.out.println(result.get(i));
    }

        public static void main(String[] args){
         Test24Point1 t24 = new Test24Point1(1, 6, 8, 6);
         t24.calculate1();
         t24.calculate2();
         t24.printResult();
        
         t24 = new Test24Point1(4, 1, 3, 8);
         t24.calculate1();
         t24.calculate2();
         t24.printResult();
        
         t24 = new Test24Point1(4, 4, 9, 6);
         t24.calculate1();
         t24.calculate2();
         t24.printResult();     t24 = new Test24Point1(2, 2, 8, 9);
         t24.calculate1();
         t24.calculate2();
         t24.printResult();
      
    int a = (int)(9 * Math.random() + 1);
    int b = (int)(9 * Math.random() + 1);
    int c = (int)(9 * Math.random() + 1);
    int d = (int)(9 * Math.random() + 1);
         t24 = new Test24Point1(a, b, c, d);
         t24.calculate1();
         t24.calculate2();
         t24.printResult();
    }
    }
    如果再改一下用递归,我想可以实现任意个数,运算后的某值的算法。
      

  22.   

    我也来凑凑热闹 
    public  class TwentyFour {
      static short num[] = new short[4];
      static char op[] = new char[3];
      public static void  main(String args[]){
         short a[] = new short[4];
         a[0] = Short.parseShort( args[0] );
         a[1] = Short.parseShort( args[1] );
         a[2] = Short.parseShort( args[2] );
         a[3] = Short.parseShort( args[3] );
         
         
         for(int i = 0 ; i < 4 ; i ++){
           for(int j = 0 ; j < 4 ; j ++){
             if(j == i) continue;
             for(int k = 0 ; k < 4 ; k ++){
               if(k == i || k == j) continue;
               for(int p = 0 ; p < 4 ; p ++){
                 if(p == k || p == j || p==i ) continue;
                 num[0] = a[i];
                 num[1] = a[j];
                 num[2] = a[k];
                 num[3] = a[p]; 
                 next((int)num[0] ,0);
               }
             }
           }
         }
      }
         static void next(int current,  int count){
            if( count >= 3){ 
               if(current == 24){
                  write();
               }
               return;
            }   
            
            int n = num[count + 1];
            op[count] = '*';
            next(current * n, count + 1);
            if( (n != 0) && (current > n) && ((current % n) == 0) ){
               op[count] = '/';
               next(current / n , count + 1);
            }
            op[count] = '+';
            next(current + n, count + 1);  
            op[count] = '-';
            next(current - n , count + 1);
         }
         
         static void write(){
           System.out.println("((" + num[0] + op[0] + num[1] + ")" + op[1] + num[2] + ")"+ op[2] + num[3]  );
         }
    }
      

  23.   


    public  class TwentyFour {
      static short num[] = new short[4];
      static char op[] = new char[3];
      public static void  main(String args[]){
         short a[] = new short[4];
         a[0] = Short.parseShort( args[0] );
         a[1] = Short.parseShort( args[1] );
         a[2] = Short.parseShort( args[2] );
         a[3] = Short.parseShort( args[3] );
         
         
         for(int i = 0 ; i < 4 ; i ++){
           for(int j = 0 ; j < 4 ; j ++){
             if(j == i) continue;
             for(int k = 0 ; k < 4 ; k ++){
               if(k == i || k == j) continue;
               for(int p = 0 ; p < 4 ; p ++){
                 if(p == k || p == j || p==i ) continue;
                 num[0] = a[i];
                 num[1] = a[j];
                 num[2] = a[k];
                 num[3] = a[p]; 
                 next((int)num[0] ,0);
                 if( (num[0] + num[1] ) * (num[2] + num[3] ) == 24)
                   writeTwo(num[0],'+',num[1],'*',num[2],'+',num[3]);
                 if( (num[0] - num[1] ) * (num[2] + num[3] ) == 24)
                   writeTwo(num[0],'-',num[1],'*',num[2],'+',num[3]);
               }
             }
           }
         }
      }
         static void next(int current,  int count){
            if( count >= 3){ 
               if(current == 24){
                  write();
               }
               return;
            }   
            
            int n = num[count + 1];
            op[count] = '*';
            next(current * n, count + 1);
            if( (n != 0) && (current > n) && ((current % n) == 0) ){
               op[count] = '/';
               next(current / n , count + 1);
            }
            op[count] = '+';
            next(current + n, count + 1);  
            op[count] = '-';
            next(current - n , count + 1);
         }
         
         static void writeTwo(int num1, char op1,int num2 ,char op2,int num3 ,char op3,int num4){
           System.out.println("(" + num1 + op1 + num2 + ")" + op2 + "(" + num3 + op3 + num4 + ")");                                        
         }  
         
         static void write(){
           System.out.println("((" + num[0] + op[0] + num[1] + ")" + op[1] + num[2] + ")"+ op[2] + num[3]  );
         }
    }现在好了  就是效率低了点  可扩展性也不好
      

  24.   

    yulin001122() :
      很简洁,效率没问题,但是逻辑有点漏洞,你第一个程序根本没考虑(a@b)@(c@d)这类的情况(@暂表示四则运算之一)如:(2+2)*(3+3),所以在第二个程序里用if判断后调用writeTwo()来弥补。但还有一种等式类型你没有判断:a@(b@c@d),如6/(1-3/4),8/(3-8/3),需要再添几个if判断然后调writeOthers()吧。
     
      或者再自定义两个运算符吧,a--b表示b-a,a//b表示b/a,就不用增加那么多判断了,不过就变成六则运算了,呵呵,能不能这么说不知道。