# 高分求24点算法

xdt2003 2004-05-07 04:53:37

...全文
19 7 点赞 打赏 收藏 举报

7 条回复

xdt2003 2004-05-08

• 打赏
• 举报

vzxq 2004-05-07

• 打赏
• 举报

daou101 2004-05-07
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;
}
// 取得结果数目
public short getResultNumber(){
return this.ResultNumber;
}
// 设置结果数目
private void setResultNumber( short number ){
this.ResultNumber = number;
}
// 增加1个结果数目
this.ResultNumber ++;
}
}
• 打赏
• 举报

daou101 2004-05-07
// 主函数
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 );
• 打赏
• 举报

daou101 2004-05-07
JAVA版的，更容易理解，如果这都没有办法看懂，我建议你从新学习一下相关语言：
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();
}

• 打赏
• 举报

xdt2003 2004-05-07

• 打赏
• 举报

daou101 2004-05-07

24点 算法 zz

24点 算法

// RESULT.cpp : Defines the entry point for the console application.
//
//穷尽所有可能的组合
//对每种组合做+,-,* ,/
//不同的运算
#include <stdafx.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <malloc.h>
#define MAX 4
#define RESULT 24
int a[MAX]={5,4,3,2};//原始数据
int A[MAX];//采用的数据相当于stack
int b[MAX];//不同层次，对与a中数据是否已经使用
char c[MAX];//保存符号
int bFound;//是否发现
int bPrev=0;
void tf(float x,int l);//进行四种运算
void out(float y,int l);//判断是继续运算，还是输出结果
char *PFormat();
int main(int argc, char* argv[])
{

int i;
for(i=0;i<MAX;i++)//初始化
A=0;
bFound=0;

tf(0,0);
if(bFound==0)
{
printf("\n没有发现");
}
getchar();

return 0;
}
void tf(float x,int l)
//x,上一次运算的结果
//l,当前所在运算的步数，也即层次
{
int i;
float y;
int B[MAX];
for(i=0;i<MAX;i++)
B=0;

for(i=0;i<MAX;i++)
{
if(b==0 && B==0)
{
A[l]=a;
b=1;
B=1;
if(l==0)//层次为0，为第一个数
{
c[l]=' ';
y=a;
out(y,l+1);
}
else{
c[l]='+';
y=x+a;
out(y,l+1);
c[l]='-';
y=x-a;
out(y,l+1);
c[l]='*';
y=x*a;
out(y,l+1);
c[l]='/';
y=x/a;
out(y,l+1);
}
b=0;

}
}

}
void out(float y,int l)
//y,上一次运算的结果
//l,当前所在运算的步数，也即层次
{
int i;
char *p;
if(l==MAX)//层次为MAX,已运算到最后，不需继续
{
if(y==RESULT)//结果对吗？
{
bFound=1;
printf("\nFound\n");
// for(i=0;i<MAX;i++)
// {
//
// printf(" %c %d",c,A);
// }
p=PFormat();
printf("%s=%d",p,RESULT);//格式化输出

getchar();
}
}
else
{
tf(y,l);//继续运算
}

}
//格式化输出
//当由+,-转向*,/需要加()
char * PFormat()
{

char tmp[1024];
//(char *)malloc((MAX-2)*2);
static char s[1024];
//=(char *)malloc((MAX-2)*2);
int i;
memset(tmp,0x00,sizeof(tmp));
memset(s,0x00,sizeof(s));
for(i=0;i<MAX;i++)
{

if(c=='+'|c=='-')
{
bPrev=0;
sprintf(tmp,"%s",s);
}
else if (bPrev==0 && (c=='*' || c=='/'))//优先改变
{
sprintf(tmp,"(%s)",s);
bPrev=1;
}
else
{
sprintf(tmp,"%s",s);
bPrev=1;
}

sprintf(s,"%s %c %d ",tmp,c,A);

}

return s;
}
• 打赏
• 举报

VB

1.6w+

VB技术相关讨论，主要为经典vb，即VB6.0

2004-05-07 04:53