C语言程序设计24点游戏,能算出24的运算法则的代码,很急

C语言程序设计24点游戏,能算出24的运算法则的代码,很急,第1张

#include<iostream>

#include<math.h>

using namespace std

const double MIN=1E-6

void Print(int *Rank,double *FourNum)

{

for(int i=0i<4i++)

cout<<FourNum[Rank[i]]<<" "

cout<<endl

}

void Calculate_24(int *Rank,int *FourNum,char *Oper,int i,int j,int k,bool &def)

{

double res=0

switch(i)

{

case 0:

res=FourNum[Rank[0]]+FourNum[Rank[1]]

break

case 1:

res=FourNum[Rank[0]]-FourNum[Rank[1]]

break

case 2:

res=FourNum[Rank[0]]*FourNum[Rank[1]]

break

case 3:

res=FourNum[Rank[0]]/FourNum[Rank[1]]

break

}

switch(j)

{

case 0:

res=res+FourNum[Rank[2]]

break

case 1:

res=res-FourNum[Rank[2]]

break

case 2:

res=res*FourNum[Rank[2]]

break

case 3:

res=res/FourNum[Rank[2]]

break

}

switch(k)

{

case 0:

res=res+FourNum[Rank[3]]

break

case 1:

res=res-FourNum[Rank[3]]

break

case 2:

res=res*FourNum[Rank[3]]

break

case 3:

res=res/FourNum[Rank[3]]

break

}

if(fabs(res-24)>MIN)

return

else

{

def=true

for(int num=1num<=7num++)

{

switch(num)

{

case 1:

cout<<FourNum[Rank[0]]

break

case 3:

cout<<FourNum[Rank[1]]

break

case 5:

cout<<FourNum[Rank[2]]

break

case 7:

cout<<FourNum[Rank[3]]

break

case 2:

cout<<Oper[i]

break

case 4:

cout<<Oper[j]

break

case 6:

cout<<Oper[k]

break

}

}

cout<<endl

}

}

void SearchTree(int Depth,int *Rank,int *FourNum,char *Oper,bool &def)

{

int i,j,k

if(Depth==4)

{

for(i=0i<4i++)

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

for(k=0k<4k++)

Calculate_24(Rank,FourNum,Oper,i,j,k,def)

}

else

{

for(i=0i<4i++)

{

int Remember=0

for(j=0j<Depthj++)

{

if(Rank[j]==i)

Remember=1

}

if(Remember)

continue

Rank[Depth]=i

SearchTree(Depth+1,Rank,FourNum,Oper,def)

}

}

}

int main()

{

int a[4],b[4],time

char c[4]={'+','-','*','/'}

bool def=false

cin>>time

while(time--)

{

for(int i=0i<4i++)//输入测试数据

cin>>a[i]

cout<<"所有可能的结果:"<<endl

SearchTree(0,b,a,c,def)

if(def==false)

cout<<"No"<<endl

}

return 0

}

#include <stdio.h>

#include <string.h>

int c24

char expr3[80]

char expr4[80]

void count2( int a, int b )

{

if ( a + b == 24 )

{

c24 ++

printf( "%s\t%s\t%d+%d=24\n", expr4, expr3, a, b )

}

if ( a * b == 24 )

{

c24 ++

printf( "%s\t%s\t%d+%d=24\n", expr4, expr3, a, b )

}

if ( a * b == 24 )

{

c24 ++

printf( "%s\t%s\t%d*%d=24\n", expr4, expr3, a, b )

}

if ( a - b == 24 )

{

c24 ++

printf( "%s\t%s\t%d-%d=24\n", expr4, expr3, a, b )

}

if ( b - a == 24 )

{

c24 ++

printf( "%s\t%s\t%d-%d=24\n", expr4, expr3, b, a )

}

if ( (b!=0) &&(a==24*b) )

{

c24 ++

printf( "%s\t%s\t%d/%d=24\n", expr4, expr3, a, b )

}

if ( (a!=0) &&(b==24*a) )

{

c24 ++

printf( "%s\t%s\t%d/%d=24\n", expr4, expr3, b, a )

}

}

void count3( int a, int b, int c )

{

int dd[3]

int i,j,k,nd

dd[0] = a

dd[1] = b

dd[2] = c

for ( i = 0i <3i++ )

for ( j = i+1j <3j++ )

{

k = 3 - i - j

nd = dd[i] + dd[j]

sprintf( expr3, "%d+%d=%d", dd[i], dd[j], nd )

count2( nd, dd[k] )

nd = dd[i] * dd[j]

sprintf( expr3, "%d*%d=%d", dd[i], dd[j], nd )

count2( nd, dd[k] )

if ( dd[i]>dd[j] )

{

nd = dd[i] - dd[j]

sprintf( expr3, "%d-%d=%d", dd[i], dd[j], nd )

}

else

{

nd = dd[j] - dd[i]

sprintf( expr3, "%d-%d=%d", dd[j], dd[i], nd )

}

count2( nd, dd[k] )

if ( (dd[j]!=0) &&(dd[i]%dd[j]==0) )

{

nd = dd[i] / dd[j]

sprintf( expr3, "%d/%d=%d", dd[i], dd[j], nd )

count2( nd, dd[k] )

}

else if ( (dd[i]!=0) &&(dd[j]%dd[i]==0) )

{

nd = dd[j] / dd[i]

sprintf( expr3, "%d/%d=%d", dd[j], dd[i], nd )

count2( nd, dd[k] )

}

}

}

void count4( int a, int b, int c, int d )

{

int dd[4]

int i,j,k,m,nddd[0] = a

dd[1] = b

dd[2] = c

dd[3] = d

for ( i = 0i <4i++ )

for ( j = i+1j <4j++ )

{

for ( k = 0(k==i) || (k==j)k++ )

m = 6 - i - j - k

nd = dd[i] + dd[j]

sprintf( expr4, "%d+%d=%d", dd[i], dd[j], nd )

count3( nd, dd[k], dd[m] )

nd = dd[i] * dd[j]

sprintf( expr4, "%d*%d=%d", dd[i], dd[j], nd )

count3( nd, dd[k], dd[m] )

if ( dd[i]>dd[j] )

{

nd = dd[i] - dd[j]

sprintf( expr4, "%d-%d=%d", dd[i], dd[j], nd )

}

else

{

nd = dd[j] - dd[i]

sprintf( expr4, "%d-%d=%d", dd[j], dd[i], nd )

}

count3( nd, dd[k], dd[m] )

if ( (dd[j]!=0) &&(dd[i]%dd[j]==0) )

{

nd = dd[j] / dd[i]

sprintf( expr4, "%d/%d=%d", dd[j], dd[i], nd )

count3( nd, dd[k], dd[m] )

}

}

}

main()

{

int a,b,c,d

printf( "Input 4 Number for Count 24: " )

scanf( "%d%d%d%d", &a, &b, &c, &d )

c24 = 0

count4( a, b, c, d )

if ( c24==0 )

printf( "=======failed======\n" )/***/

printf("End of C Program, Press Enter.\n")

getchar()

getchar()

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7766380.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存