计算24点的穷举程序





#include
#include

void list24P(double a,double b,double c,double d);

void main(void)
{
int a,b,c,d;
int rangeFlag;

printf("nThe game 24 points.n");
printf("nEnter a b c d:");
cin>>a;
cin>>b;
cin>>c;
cin>>d;
printf("na=%d,b=%d,c=%d,d=%d",a,b,c,d);
printf("n");

rangeFlag=0;
if((a10))
{
printf("na should be an integer in the range[1,10].");
rangeFlag=1;;
}

if((b10))
{
printf("nb should be an integer in the range[1,10].");
rangeFlag=1;
}

if((c10))
{
printf("nc should be an integer in the range[1,10].");
rangeFlag=1;
}

if((d10))
{
printf("nc should be an integer in the range[1,10].");
rangeFlag=1;
}
if(1==rangeFlag)
{
return;
}

double x[4],y[4];
int i,j;
int index;
int PP[24][4]={
{1,2,3,4},
{2,1,3,4},
{1,3,2,4},
{2,3,1,4},

{3,1,2,4},
{3,2,1,4},
{1,2,4,3},
{2,1,4,3},

{1,3,4,2},
{2,3,4,1},
{3,1,4,2},
{3,2,4,1},

{1,4,2,3},
{2,4,1,3},
{1,4,3,2},
{2,4,3,1},

{3,4,1,2},
{3,4,2,1},
{4,1,2,3},
{4,2,1,3},

{4,1,3,2},
{4,2,3,1},
{4,3,1,2},
{4,3,2,1}
};

x[0]=double(a+1e-9);//process the case of division of 0
x[1]=double(b+2e-9);
x[2]=double(c+3e-9);
x[3]=double(d+4e-9);

for(i=0;i23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x23.99)&&(x<24.01))
{
printf("n(%d / %d )/ (%d / %d) = %g",int(a),int(b),int(c),int(d),x);
}
}




[ 编辑者 judy_cn 于日期 06May03 ]

Paste it again:Part two





//编译并执行该程序(part two)

//用重定向方式将结果输出到文件

//将生成的文件的所有内容拷贝到Part one的后面即形成完整的穷举24点计算式的程序

//但该程序有一个缺点,没有去除重复的算式

//感谢sean9991的建议



#include < stdio.h >



void main(void)

{



int L,k;

int N;

int i,j;

int index;

int a;



L=4;//+,-,,/ totally 4 operations

k=3;//totally 3 positions available. For example, for a
bcd, we have three positions to fill the operation symbol



char sym[64][3];

char symTable[4]={’+’,’-’,’’,’/’};

char buf[200];



N=1;

for(i=0;i < k;i++)

{

N=N
L;

}



// 0000,0001,0002,0003,0010,0011,0012,0013,…,3330,3331,3332,3333

// ++++,++±,+++,+++/,…,///,////

for(i=0;i < N;i++)

{

index=i;

for(j=0;j < k;j++)

{

a=index%4;

sym[i][j]=symTable[a];



index -=a;

index /=4;

}

}





printf(“n/List all the cases for the 24 points game./”);

printf(“nvoid list24P(double a,double b,double c,double d)”);

printf(“n{”);

printf(“n double x;”);

printf(“nn”);



//abcd

printf("nn //Type:a
bcd");

for(i=0;i < N;i++)

{

printf(“n x = a %c b %c c %c d;”,sym[i][0],sym[i][1],sym[i][2]);

printf(“n if((x > 23.99)&&(x < 24.01))n {”);



sprintf(buf," printf("\n%%d %c %%d %c %%d %c %%d = %%g",int(a),int(b),int(c),int(d),x);",sym[i][0],sym[i][1],sym[i][2]);



printf(“n %s”,buf);

printf(“n }”);



}



//(a)(bcd)

printf("nn //Type:(a)
(bcd)");

for(i=0;i < N;i++)

{

printf(“n x = (a) %c (b %c c %c d);”,sym[i][0],sym[i][1],sym[i][2]);

printf(“n if((x > 23.99)&&(x < 24.01))n {”);



sprintf(buf," printf("\n(%%d) %c (%%d %c %%d %c %%d) = %%g",int(a),int(b),int(c),int(d),x);",sym[i][0],sym[i][1],sym[i][2]);



printf(“n %s”,buf);

printf(“n }”);



}



//(a)(b(cd))

printf("nn //Type:(a)
(b*(cd))");

for(i=0;i < N;i++)

{

printf(“n x = (a) %c (b %c (c %c d));”,sym[i][0],sym[i][1],sym[i][2]);

printf(“n if((x > 23.99)&&(x < 24.01))n {”);



sprintf(buf," printf("\n(%%d) %c (%%d %c (%%d %c %%d)) = %%g",int(a),int(b),int(c),int(d),x);",sym[i][0],sym[i][1],sym[i][2]);



printf(“n %s”,buf);

printf(“n }”);



}



//(a)
((bc)d)

printf("nn //Type:(a)
((b
c)d))");

for(i=0;i < N;i++)

{

printf(“n x = (a) %c ((b %c c )%c d);”,sym[i][0],sym[i][1],sym[i][2]);

printf(“n if((x > 23.99)&&(x < 24.01))n {”);



sprintf(buf," printf("\n(%%d) %c ((%%d %c %%d) %c %%d) = %%g",int(a),int(b),int(c),int(d),x);",sym[i][0],sym[i][1],sym[i][2]);



printf(“n %s”,buf);

printf(“n }”);



}





//(a
bc)d

printf("nn //Type:(a
b
c)d");

for(i=0;i < N;i++)

{

printf(“n x = (a %c b %c c )%c d;”,sym[i][0],sym[i][1],sym[i][2]);

printf(“n if((x > 23.99)&&(x < 24.01))n {”);



sprintf(buf," printf("\n(%%d %c %%d %c %%d )%c %%d = %%g",int(a),int(b),int(c),int(d),x);",sym[i][0],sym[i][1],sym[i][2]);



printf(“n %s”,buf);

printf(“n }”);



}





//(a
(bc))d

printf("nn //Type:a
b
cd");

for(i=0;i < N;i++)

{

printf(“n x = (a %c (b %c c)) %c d;”,sym[i][0],sym[i][1],sym[i][2]);

printf(“n if((x > 23.99)&&(x < 24.01))n {”);



sprintf(buf," printf("\n(%%d %c (%%d %c %%d)) %c %%d = %%g",int(a),int(b),int(c),int(d),x);",sym[i][0],sym[i][1],sym[i][2]);



printf(“n %s”,buf);

printf(“n }”);



}





//((a
b)c)d

printf("nn //Type:((a
b)c)d");

for(i=0;i < N;i++)

{

printf(“n x = ((a %c b) %c c) %c d;”,sym[i][0],sym[i][1],sym[i][2]);

printf(“n if((x > 23.99)&&(x < 24.01))n {”);



sprintf(buf," printf("\n((%%d %c %%d) %c %%d )%c %%d = %%g",int(a),int(b),int(c),int(d),x);",sym[i][0],sym[i][1],sym[i][2]);



printf(“n %s”,buf);

printf(“n }”);



}





//(a
b)
(c
d)

printf(“nn //Type:(ab)(c*d)”);

for(i=0;i < N;i++)

{

printf(“n x = (a %c b )%c (c %c d);”,sym[i][0],sym[i][1],sym[i][2]);

printf(“n if((x > 23.99)&&(x < 24.01))n {”);



sprintf(buf," printf("\n(%%d %c %%d )%c (%%d %c %%d) = %%g",int(a),int(b),int(c),int(d),x);",sym[i][0],sym[i][1],sym[i][2]);



printf(“n %s”,buf);

printf(“n }”);



}





printf(“n}”);



}



Paste it again art one







//程序第一部分

//感谢sean9991

#include < stdio.h >

#include < iostream.h >



void list24P(double a,double b,double c,double d);



void main(void)

{

int a,b,c,d;

int rangeFlag;



printf(“nThe game 24 points.n”);

printf(“nEnter a b c d:”);

cin > > a;

cin > > b;

cin > > c;

cin > > d;

printf(“na=%d,b=%d,c=%d,d=%d”,a,b,c,d);

printf(“n”);



rangeFlag=0;

if((a < 1)||(a > 10))

{

printf(“na should be an integer in the range[1,10].”);

rangeFlag=1;;

}



if((b < 1)||(b > 10))

{

printf(“nb should be an integer in the range[1,10].”);

rangeFlag=1;

}



if((c < 1)||(c > 10))

{

printf(“nc should be an integer in the range[1,10].”);

rangeFlag=1;

}



if((d < 1)||(d > 10))

{

printf(“nc should be an integer in the range[1,10].”);

rangeFlag=1;

}

if(1==rangeFlag)

{

return;

}



double x[4],y[4];

int i,j;

int index;

int PP[24][4]={

{1,2,3,4},

{2,1,3,4},

{1,3,2,4},

{2,3,1,4},



{3,1,2,4},

{3,2,1,4},

{1,2,4,3},

{2,1,4,3},



{1,3,4,2},

{2,3,4,1},

{3,1,4,2},

{3,2,4,1},



{1,4,2,3},

{2,4,1,3},

{1,4,3,2},

{2,4,3,1},



{3,4,1,2},

{3,4,2,1},

{4,1,2,3},

{4,2,1,3},



{4,1,3,2},

{4,2,3,1},

{4,3,1,2},

{4,3,2,1}

};



x[0]=double(a+1e-9);//process the case of division of 0

x[1]=double(b+2e-9);

x[2]=double(c+3e-9);

x[3]=double(d+4e-9);



for(i=0;i < 24;i++)

{

for(j=0;j < 4;j++)

{

index=PP[i][j]-1;

y[j]=x[index];

}

list24P(y[0],y[1],y[2],y[3]);

}



}





看起来像是C语言,弱

怎么不用c++?

想学点C,你这24点是啥意思.C在哪执行拉?

菜鸟取经.


于 2003-05-06 01:25, judy_cn 写:

Paste it again art one







//程序第一部分

//感谢sean9991

#include < stdio.h >

#include < iostream.h >



void list24P(double a,double b,double c,double d);



void main(void)

{

int a,b,c,d;

int rangeFlag;



printf(“nThe game 24 points.n”);

printf(“nEnter a b c d:”);

cin > > a;

cin > > b;

cin > > c;

cin > > d;

printf(“na=%d,b=%d,c=%d,d=%d”,a,b,c,d);

printf(“n”);



rangeFlag=0;

if((a < 1)||(a > 10))

{

printf(“na should be an integer in the range[1,10].”);

rangeFlag=1;;

}



if((b < 1)||(b > 10))

{

printf(“nb should be an integer in the range[1,10].”);

rangeFlag=1;

}



if((c < 1)||(c > 10))

{

printf(“nc should be an integer in the range[1,10].”);

rangeFlag=1;

}



if((d < 1)||(d > 10))

{

printf(“nc should be an integer in the range[1,10].”);

rangeFlag=1;

}

if(1==rangeFlag)

{

return;

}



double x[4],y[4];

int i,j;

int index;

int PP[24][4]={

{1,2,3,4},

{2,1,3,4},

{1,3,2,4},

{2,3,1,4},



{3,1,2,4},

{3,2,1,4},

{1,2,4,3},

{2,1,4,3},



{1,3,4,2},

{2,3,4,1},

{3,1,4,2},

{3,2,4,1},



{1,4,2,3},

{2,4,1,3},

{1,4,3,2},

{2,4,3,1},



{3,4,1,2},

{3,4,2,1},

{4,1,2,3},

{4,2,1,3},



{4,1,3,2},

{4,2,3,1},

{4,3,1,2},

{4,3,2,1}

};



x[0]=double(a+1e-9);//process the case of division of 0

x[1]=double(b+2e-9);

x[2]=double(c+3e-9);

x[3]=double(d+4e-9);



for(i=0;i < 24;i++)

{

for(j=0;j < 4;j++)

{

index=PP[i][j]-1;

y[j]=x[index];

}

list24P(y[0],y[1],y[2],y[3]);

}



}







你到底是学什么,干什么的?