导航:首页 > 源码编译 > 24点的算法c语言

24点的算法c语言

发布时间:2023-12-11 12:04:07

❶ C语言24点的算法

下面是我自己写的一个程序:

我的解法是把这个问题分解成了两个子问题,首先求出4个数字的无重复全排列,放到一个数组里面,再对没一个排列情况,从头到尾穷举所有的四则运算情况。注意到除法是特殊的,我用x/y表示x除以y,用x|y表示x分之y。注意到,如果穷举的解得到-24的话,只需要把有减法的地方调换一下顺序就可以了,代码如下
/***********************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int index[4]={0,1,2,3};//used to generate subscription collection
int sub[4]; //used in p() only
float f[4]={8.0f,3.0f,3.0f,8.0f};//the 24 point numbers
float fs[24][4];//all possible permutaions of f
float tmp[4]; //used for buf
int g_number=0; //number of permutations
float RES[4];
char op[3];
void p(int idx){//求全排列的函数
if(idx==4){
for(int i=0;i<4;++i){tmp[i]=f[sub[i]];}
for(int g=0;g<g_number;++g){if(memcmp(fs[g],tmp,sizeof(float)*4)==0)return;}
for(int i=0;i<4;++i){fs[g_number][i]=f[sub[i]];}
g_number++;
return;
}
for(int i=0;i<4;++i){//make subscription collections
bool pflag=false;
for(int j=0;j<idx;++j){if(sub[j]==i)pflag=true;}
if(pflag==true)continue;
sub[idx]=index[i];
p(idx+1);
}
}
void solve(int L){//对某个排列,递归求所有四则运算的结果,找到就退出
if(L==3){
if(fabs(fabs(RES[L])-24.0f)<0.01f){
printf("Found solution,RES=%f,((%d%c%d)%c%d)%c%d\n",RES[L],
(int)f[0],op[0],
(int)f[1],op[1],
(int)f[2],op[2],
(int)f[3]);
exit(0);
}
return;
}
for(int j=0;j<5;++j){//j judges for operators
if(j==0){RES[L+1]=RES[L]+tmp[L+1];op[L]='+';solve(L+1);}
if(j==1){RES[L+1]=RES[L]-tmp[L+1];op[L]='-';solve(L+1);}
if(j==2){RES[L+1]=RES[L]*tmp[L+1];op[L]='*';solve(L+1);}
if(j==3&&tmp[L+1]!=0)
{RES[L+1]=RES[L]/tmp[L+1];op[L]='/';solve(L+1);}
if(j==4&&RES[L+1]!=0)
{RES[L+1]=tmp[L+1]/RES[L];op[L]='|';solve(L+1);}
}
}
int main(int argc,char* argv[]){//should avoid 0
f[0]=atoi(argv[1]);
f[1]=atoi(argv[2]);
f[2]=atoi(argv[3]);
f[3]=atoi(argv[4]);
p(0);
for(int i=0;i<g_number;++i){
memcpy(tmp,fs[i],sizeof(float)*4);
RES[0]=tmp[0];
for(int t=0;t<4;++t){ printf("%d,",(int)tmp[t]); }
printf("\n");
solve(0);
}
printf("Found no solution :( \n");
return 0;
}

----------编译运行,运行时的参数就是4个数字
g++ p.cpp && ./a.out 1 5 5 5
1,5,5,5,
Found solution,RES=-24.000000,((1/5)-5)*5
g++ p.cpp && ./a.out 8 3 3 8
8,3,3,8,
Found solution,RES=-24.000006,((8/3)-3)|8
上面这个解写出来就是
8
--------- = 24
3-(8/3)
主程序为了简化,省去了对输入的检查,楼主可以自己添加。

❷ 24点游戏C语言

#include<cstdlib>#include<iostream>#include<ctime>using namespace std;
class CCard{private: int m_Pip[5];//一共五张牌 int m_Number;//发了多少张牌 int m_Dollar;//赌本 int m_Gamble;//赌注 int m_Win;//赢局数 int m_Lose;//输局数 int m_Draw;//平局数public: CCard();//构造函数。 void FirstPlayTwo();//最初的两张牌 int GetNumber();//返回牌张 int GetPip();//返回点数 void DisplayPip();//依次全部显示牌面的点数 void DisplayPip(int);//除了第一张牌,依次显示全部牌面点数(针对计算机牌的显示) void TurnPlay();//出一张牌。 void Win();//赢了计算赌注 void Lose();//输了 void Draw();//平局 int SetGamble(int);//设置赌本,赌本不够返回-1 int GetMoney();//返回钱数 void DisplayInfo();//打印必要的信息 int GetCurrentCard();//返回当前的牌点};
CCard::GetNumber(){ return m_Number;}
CCard::CCard()//构造函数,初始化{ m_Number = 0; m_Dollar = 100;//初始赌注为100美元 for(int i=0;i<5;i++) m_Pip[i] = 0; m_Gamble = 0; m_Win = m_Lose = m_Draw = 0;}
int CCard::GetMoney(){ return m_Dollar;}
void CCard::DisplayInfo()//打印必要的信息{ cout<<"\n\n\n\t\t\t您一共玩了"<<m_Win+m_Lose+m_Draw<<"局 "<<"赢了"<<m_Win<<"局 "<<"输了"<<m_Lose<<"局 "<<"平局"<<m_Draw<<"次。"<<endl; cout<<"\n\t\t\t\t您的赌本共计有$"<<m_Draw<<"。\n"<<endl;}
int CCard::SetGamble(int gamble){ if(gamble<0) { cout<<"\n输入金额有误"<<endl; return -1; } if(m_Dollar-gamble<0) { cout<<"\n金额不足"<<endl; return -1; } else m_Gamble = gamble; m_Dollar -= gamble; return 0;}
void CCard::FirstPlayTwo()//最初两张牌{ m_Pip[0] = rand()%13+1; m_Pip[1] = rand()%13+1; m_Number = 2;}
int CCard::GetPip(){ int SumPip = 0; for(int i=0;i<m_Number;i++) { SumPip += m_Pip[i]; } return SumPip;}
void CCard::DisplayPip(){ int i; for(i=0;i<m_Number;i++) { cout<<m_Pip[i]<<'\t'; } cout<<endl;}
void CCard::TurnPlay(){ m_Number++; m_Pip[m_Number-1] = rand()%13+1;}
void CCard::Win(){ cout<<"赢家牌面:"; DisplayPip(); cout<<"\n牌面点数:"<<GetPip()<<endl; m_Dollar = m_Dollar + 2 * m_Gamble; m_Win++; cout<<"\n赌本:$"<<m_Dollar<<" 赢了"<<m_Win<<"次 "<<"输了"<<m_Lose<<"次 "<<"平局"<<m_Draw<<"次"<<endl; cout<<endl; cout<<endl;}
void CCard::Lose(){ m_Lose++; cout<<"\n输家的牌面:"; DisplayPip(); if(GetPip()>21) cout<<"\t\t\t\t\t\t\t\t暴了!"<<endl; else cout<<"牌面点数:"<<GetPip()<<endl; cout<<"\n赌本:$"<<m_Dollar<<" 赢了"<<m_Win<<"次 "<<"输了"<<m_Lose<<"次 "<<"平局"<<m_Draw<<"次"<<endl; cout<<endl<<endl;}
void CCard::Draw(){ m_Draw++; m_Dollar += m_Gamble; cout<<"\n平局牌面:"; DisplayPip(); if(GetPip()>21) cout<<"\n暴了!"<<endl; else cout<<"牌面点数:"<<GetPip()<<endl; cout<<"赌本:$"<<m_Dollar<<" 赢了"<<m_Win<<"次 "<<"输了"<<m_Lose<<"次 "<<"平局"<<m_Draw<<"次"<<endl; cout<<endl<<endl;}
void DisplayRule(void){ cout<<endl<<endl; cout<<"\t※※※※※※※※※※欢迎进入21点游戏世界!※※※※※※※※※※\n\n"; cout<<"\t\t 游戏规则:\n"; cout<<endl; cout<<"\t\t 1.玩家最多可以要5张牌;\n"; cout<<endl; cout<<"\t\t 2.如果牌点数的总数超过21点则暴点,自动判数;\n"; cout<<endl; cout<<"\t\t 3.赢家可得双倍的赌注;\n"; cout<<endl; cout<<"\t\t 4.计算机方在大于等于16点时不再要牌。\n"; cout<<endl; cout<<"\t※※※※※※※※※※※※※ 祝您好运! ※※※※※※※※※※\n"; cout<<endl<<endl;}
void Judge(CCard &cpu,CCard &player){ cout<<endl; if((cpu.GetPip()>21&&player.GetPip()>21)||cpu.GetPip()==player.GetPip()) { cout<<"\n\n\t\t\t\t\t\t\t\t平局!\n"; cout<<"计算机数据:\t"; cpu.DisplayPip(); cout<<"牌面点数:"<<cpu.GetPip()<<endl; cout<<"\n您的数据:\t"; player.Draw(); cout<<endl; } else if((cpu.GetPip()>21)||(player.GetPip()>cpu.GetPip()&&player.GetPip()<=21)) { cout<<"\n\n\n\t\t\t\t\t\t\t\t恭喜您赢了!\n\n"; cout<<"计算机数据:\t"; cpu.DisplayPip(); cout<<"牌面点数:"<<cpu.GetPip()<<endl; cout<<"\n您的数据:\t"; player.Win(); cout<<endl; } else { cout<<"\n\n\t\t\t\t\t\t\t\t很遗憾您输了!\n"; cout<<"计算机数据:\t"; cpu.DisplayPip(); cout<<"牌面点数:"<<cpu.GetPip()<<endl; cout<<"\n您的数据:\t"; player.Lose(); cout<<endl; }}void CCard::DisplayPip(int n){ int i; cout<<"[*]"<<'\t'; for(i=1;i<m_Number;i++) cout<<m_Pip[i]<<'\t'; cout<<endl;}
void PlayTurn(CCard &cpu,CCard & player)//玩一局{ char chChoice; int blCpu = 1;//判断是否要牌 int blPlayer = 1; cpu.FirstPlayTwo();//计算机和玩家各要两张牌 player.FirstPlayTwo(); do { cout<<"\n您的牌点为:\t"; player.DisplayPip(); cout<<"\n"; cout<<"您的牌面点数是:"<<player.GetPip()<<endl; cout<<"\n计算机的牌点为:\t"; cpu.DisplayPip(1); if(blPlayer) { cout<<"\n\n\t您是否继续要牌(Y/N)?\t\t\t"; cin>>chChoice; if((chChoice == 'Y'||chChoice == 'y')) { if(player.GetNumber()<5) { player.TurnPlay(); cout<<"\n您要的这张牌是:"<<player.GetPip()<<endl; if(player.GetPip()>21) blPlayer = 0; } else { cout<<"对不起,您已经要了5张牌,不能再要牌了!"; blPlayer = 0; } } if(chChoice=='n'||chChoice=='N') blPlayer = 0; } if(cpu.GetPip()<16&&cpu.GetNumber()<5) { cpu.TurnPlay(); cout<<"\n计算机要牌,牌点是:"<<cpu.GetPip()<<endl; } else blCpu = 0; if(blCpu&&player.GetNumber()<5&&player.GetPip()<21) blPlayer = 1; }while(blCpu||blPlayer); Judge(cpu,player); return ;}
int main(){ srand((unsigned)time(NULL));//初始化随机数种子 CCard cpu,player; int blLogic; int nMoney;// DisplayRule();// char chChoice; cout<<"是否现在开始游戏(Y/N)?\t\t"; cin>>chChoice; while(chChoice=='Y'||chChoice=='y') { do { cout<<endl; cout<<"\t\t\t您现在有的赌本:$"<<player.GetMoney(); cout<<"\n\n请下注(赌注不能超过赌本);"; cin>>nMoney; blLogic = player.SetGamble(nMoney); if(blLogic) cout<<"您的赌本不够,请重新下注!\n"; }while(blLogic); PlayTurn(cpu,player);// cout<<"是否继续21点游戏(Y/N)?\t\t\t"; cin>>chChoice; } player.DisplayInfo(); cout<<"\t\t\t您的选择是明智的,赌博有碍家庭和睦!\n"; cout<<"\n\n\t\t\t\t欢迎再次使用此程序!"<<endl<<endl<<endl;
return 0;}

编程C语言 24点游戏

#include "stdio.h"
#include "math.h"
#include "time.h"

void main()
{
void first();
void second();
int third();
time_t t;
int close=0;
char get;
clrscr();
srand((unsigned) time(&t));
for(;;)
{
clrscr();
printf("24 POINTS SYSTEM v2.2\n");
printf("MADE BY DANTE WANG\n");
printf("Jul.28th 2006\n\n");
printf("Please choose mode!\n\n");
printf("1. Game Mode\n");
printf("2. Count Mode\n");
printf("3. Quit\n\n");
get=getch();
switch(get)
{
case '1': first(); break;
case '2': second();break;
case '3': close=third(); break;
default: printf("Please input 1, 2 or 3!\n"); getch();
}
if(close)
break;
}
}

void first()
{
int jdgsgn(int a,int b,int c,int d);
int n[4],i;
char ch[100],ch1[100];
int check(char *p);
int exam2(char *p,int a,int b,int c,int d);
void sign(int a,int b,int c,int d);
double figure(char *p);
void draw(int *p);
for(;;)
{
for(i=0;i<4;i++)
{
n[i]=rand()%13+1;
}
draw(n);
if(!jdgsgn(n[0],n[1],n[2],n[3]))
{
printf("No answer!");
getch();
continue;
}
gets(ch);
if(!strcmp(strlwr(ch),"quit"))
break;
else
{
strcpy(ch1,ch);
if(!check(ch)&&!exam2(ch,n[0],n[1],n[2],n[3])&&fabs(figure(ch1)-24)<=0.000001)
{
printf("\nGood job!\n");
getch();
}
else
{
printf("\nWrong!\n\nAnswer: ");
sign(n[0],n[1],n[2],n[3]);
getch();
}
}
}
}

void second()
{
int n[4],i;
char c[4][10];
int exam(char *p);
void sign(int a,int b,int c,int d);
double recog(char *p);
clrscr();
printf("Input 0 to leave.\n\n");
for(;;)
{
for(i=0;i<4;i++)
{
scanf("%s",&c[i]);
if(exam(c[i]))
{
printf("GRAMMER ERROR!\n");
i--;
continue;
}
n[i]=(int)recog(c[i]);
if(n[i]==0)
break;
}
if(n[0]&&n[1]&&n[2]&&n[3])
sign(n[0],n[1],n[2],n[3]);
else
break;
}
}

int third()
{
printf("Thanks for using this software!\n\n");
printf("If you have any advices or suggestions,\n\n");
printf("welcome to send email to [email protected].\n\n");
getch();
return(1);
}

void sign(int a,int b,int c,int d)
{
char *change(int x);
double figure(char *p);
int i[4],n,n1,n2,n3,n4,s1,s2,s3,t,flag=0;
char s[4][2]=,p[64]=,p1[64]=;
i[0]=a;
i[1]=b;
i[2]=c;
i[3]=d;
for(n1=0;n1<4;n1++)
{
for(n2=0;n2<4;n2++)
{
if(n2==n1)
continue;
for(n3=0;n3<4;n3++)
{
if(n3==n2||n3==n1)
continue;
for(n4=0;n4<4;n4++)
{
if(n4==n3||n4==n2||n4==n1)
continue;
for(s1=0;s1<4;s1++)
{
for(s2=0;s2<4;s2++)
{
for(s3=0;s3<4;s3++)
{
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));

作者: wyc890618 2006-8-7 15:46 回复此发言

--------------------------------------------------------------------------------

2 完美的24点运算与游戏程序
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
printf("%s\n",p);
flag++;
break;
}
*p='\0';
if(s1/2==1&&(s2/2==0||s3/2==0))
{
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,"(");
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcat(p,")");
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
printf("%s\n",p);
flag++;
break;
}
*p='\0';
}
if(s1/2==0&&s2/2==1)
{
strcat(p,"(");
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,")");
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
printf("%s\n",p);
flag++;
break;
}
*p='\0';
}
if(s2/2==0&&s1/2==1)
{
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,"(");
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,")");
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
printf("%s\n",p);
flag++;
break;
}
*p='\0';
}
if((s1/2==0||s2/2==0)&&s3/2==1)
{
strcat(p,"(");
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,")");
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
printf("%s\n",p);
flag++;
break;
}
*p='\0';
}
if(s1/2==0&&s2/2==1&&s3/2==0)
{
strcat(p,"(");
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,")");
strcat(p,s[s2]);
strcat(p,"(");
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcat(p,")");
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
printf("%s\n",p);
flag++;
break;
}
*p='\0';
}
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
if(flag==0)
printf("No answer!\n");
}

char *change(int x)
{
static char a[64];
char *c;
c=a+63;
*c='\0';
c--;
for(;;c--)
{
if(x>=10)
{
*c=x%10+'1'-1;
x=x/10;
}
else
{
*c=x+'1'-1;
break;
}
}
return&;;
}

double figure(char *p)
{
int judge(char *p);
int judge2(char *p);
void add(char *p);
void pick(char *p);
double recog(char *p);
char *c=p,*c1,c2[64];
int sign,kuoh=0,flag2=0;
double result,chu;
add(p);
for(sign=0;*c!='\0';c++)
{
if(*c=='+'||*c=='-'||*c=='*'||*c=='/'||*c=='('||*c==')')
{
sign++;
break;
}

作者: wyc890618 2006-8-7 15:46 回复此发言

--------------------------------------------------------------------------------

3 完美的24点运算与游戏程序
}
if(sign==0)
return(recog(p));
else
{
c=p;
for(;*c!='\0';c++)
{
if(*c=='(')
kuoh++;
if(*c==')')
kuoh--;
if(!judge2&;)
{
if(*c=='+'&&kuoh==0)
{
*c='\0';
result=figure(p)+figure(c+1);
flag2++;
break;
}
if(*c=='-'&&kuoh==0)
{
*c='\0';
result=figure(p)-figure(c+1);
flag2++;
break;
}
}
if(!judge&;)
{
if(*c=='*'&&kuoh==0)
{
*c='\0';
result=figure(p)*figure(c+1);
flag2++;
break;
}
if(*c=='/'&&kuoh==0)
{
*c='\0';
chu=figure(c+1);
if(chu==0)
{
result=99999;
}
else
{
result=figure(p)/chu;
}
flag2++;
break;
}
}
}
if(flag2)
return(result+0);
else
{
pick(p);
result=figure(p);
return(result+0);
}
}
}

int judge(char *p)
{
char *c=p;
int kuoh=0,flag=0;
for(c++;*c!='\0';c++)
{
if(*c=='(')
kuoh++;
if(*c==')')
kuoh--;
if(kuoh==0&&(*c=='+'||*c=='-'||*c=='*'||*c=='/'))
{
flag++;
break;
}
}
return(flag);
}

int judge2(char *p)
{
char *c=p;
int kuoh=0,flag=0;
for(c++;*c!='\0';c++)
{
if(*c=='(')
kuoh++;
if(*c==')')
kuoh--;
if(kuoh==0&&(*c=='+'||*c=='-'))
{
flag++;
break;
}
}
return(flag);
}

void add(char *p)
{
char *c=p,b[64];
int flag=0;
for(;*c!='\0';c++)
{
if((*c=='-'||*c=='+')&&(*(c+1)>='0'&&*(c+1)<='9'))
flag++;
if((*c>='0'&&*c<='9'||*c=='.')&&(*(c+1)=='*'||*(c+1)=='/'||*(c+1)=='+'||*(c+1)=='-'))
{
flag++;
break;
}
if((*c>='0'&&*c<='9'||*c=='.')&&*(c+1)==')')
{
break;
}
}
if(flag==2)
flag=1;
else
flag=0;
if(flag)
{
c=p;
for(;*c!='\0';c++)
{
if(*(c+1)>='0'&&*(c+1)<='9')
{
if(*c=='+'||*c=='-')
{
strcpy(b,c);
*c='(';
strcpy(c+1,b);
}
break;
}
}
c+=2;
for(;*c!='\0';c++)
{
if((*c<'0'||*c>'9')&&*c!='.')
{
strcpy(b,c);
*c=')';
strcpy(c+1,b);
break;
}
}
}
}

void pick(char *p)
{
char *c=p;
if(*c=='('&&*(c+strlen&;-1)==')')
{
strcpy(c,c+1);
*(c+strlen&;-1)='\0';
}
}

double recog(char *p)
{
char *c=p;
int flag=0,number;
double quan,ans=0;
for(;;c++)
{
if(*c=='.')
{
flag++;
number=c-p;
}
if(*c!='.'&&(*c<'0'||*c>'9'))
break;
}
if(flag==0)
{
for(quan=1,c--;c>=p;c--,quan*=10)
ans+=(*c-'1'+1)*quan;
}
else
{
for(c--,quan=pow(10,number-(c-p));c>=p;c--)
{
if(*c=='.')
continue;
else
{
ans+=(*c-'1'+1)*quan;
quan*=10;
}
}
}
return(ans);
}

int exam(char *p)
{
double recog(char *p);
char *c=p;
int number=0;
for(;*c!='\0';c++)
{
if(*c>'9'||*c<'0')
number++;
if(number)
break;
}
if(!number)
{
if(recog(p)>13||recog(p)<0)

作者: wyc890618 2006-8-7 15:46 回复此发言

--------------------------------------------------------------------------------

4 完美的24点运算与游戏程序
number++;
}
return(number);
}

void draw(int *p)
{
time_t t;
int n;
clrscr();
printf("Press \"quit\" to leave.");
srand((unsigned) time(&t));
for(n=0;n<4;n++)
{
gotoxy(1+7*n,3);
printf("\xda\xc4\xc4\xc4\xc4\xbf");
gotoxy(1+7*n,4);
printf("\xb3%c \xb3",rand()%4+3);
gotoxy(1+7*n,5);
printf("\xb3 \xb3");
gotoxy(1+7*n,6);
switch(*(p+n))
{
case 1:
printf("\xb3 A\xb3");
break;
case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:
printf("\xb3%4d\xb3",*(p+n));
break;
case 11:
printf("\xb3 J\xb3");
break;
case 12:
printf("\xb3 Q\xb3");
break;
case 13:
printf("\xb3 K\xb3");
}
gotoxy(1+7*n,7);
printf("\xc0\xc4\xc4\xc4\xc4\xd9");
gotoxy(1,9);
}
}

int jdgsgn(int a,int b,int c,int d)
{
char *change(int x);
double figure(char *p);
int i[4],n,n1,n2,n3,n4,s1,s2,s3,t,flag=0;
char s[4][2]=,p[64]=,p1[64]=;
i[0]=a;
i[1]=b;
i[2]=c;
i[3]=d;
for(n1=0;n1<4;n1++)
{
for(n2=0;n2<4;n2++)
{
if(n2==n1)
continue;
for(n3=0;n3<4;n3++)
{
if(n3==n2||n3==n1)
continue;
for(n4=0;n4<4;n4++)
{
if(n4==n3||n4==n2||n4==n1)
continue;
for(s1=0;s1<4;s1++)
{
for(s2=0;s2<4;s2++)
{
for(s3=0;s3<4;s3++)
{
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
flag++;
break;
}
*p='\0';
if(s1/2==1&&(s2/2==0||s3/2==0))
{
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,"(");
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcat(p,")");
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
flag++;
break;
}
*p='\0';
}
if(s1/2==0&&s2/2==1)
{
strcat(p,"(");
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,")");
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
flag++;
break;
}
*p='\0';
}
if(s2/2==0&&s1/2==1)
{
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,"(");
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,")");
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
flag++;
break;
}
*p='\0';
}
if((s1/2==0||s2/2==0)&&s3/2==1)
{
strcat(p,"(");
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,")");
strcat(p,s[s3]);
strcat(p,change(i[n4]));

strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
flag++;
break;
}
*p='\0';
}
if(s1/2==0&&s2/2==1&&s3/2==0)
{
strcat(p,"(");
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,")");
strcat(p,s[s2]);
strcat(p,"(");
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcat(p,")");
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
flag++;
break;
}
*p='\0';
}
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
return(flag);
}

int exam2(char *p,int a,int b,int c,int d)
{
double recog(char *p);
int number=0,num[4],flag=0,n,m;
num[0]=a;
num[1]=b;
num[2]=c;
num[3]=d;
for(m=0;*p!='\0';p++,m++)
{
if(*p>='0'&&*p<='9'&&(*(p+1)>'9'||*(p+1)<'0'))
number++;
if(m>0&&*p>='0'&&*p<='9'&&*(p-1)>='0'&&*(p-1)<='9')
continue;
if(*p<='9'&&*p>='0')
{
if(recog(p)>13||recog(p)<1)
{
flag++;
break;
}
else
{
for(n=0;n<4;n++)
{
if(recog(p)==num[n])
{
num[n]=0;
break;
}
}
}
}
}
if(!flag)
{
if(number!=4)
flag++;
else
if(num[1]||num[2]||num[3]||num[0])
flag++;
}
return(flag);
}

int check(char *p)
{
int kuoh=0,flag=0;
for(;*p!='\0';p++)
{
if(*p==' ')
{
flag++;
break;
}
if(kuoh<0)
{
flag++;
break;
}
if(*p=='(')
kuoh++;
if(*p==')')
kuoh--;
if((*p=='+'||*p=='-'||*p=='*'||*p=='/')&&(*(p+1)=='*'||*(p+1)=='/'||*(p+1)=='+'||*(p+1)=='-'))
{
flag++;
break;
}
if(*p=='('&&(*(p+1)>'9'||*(p+1)<'0'&&*(p+1)!='-'))
{
flag++;
break;
}
if(*p==')'&&*(p+1)!='+'&&*(p+1)!='-'&&*(p+1)!='*'&&*(p+1)!='/'&&*(p+1)!='\0')
{
flag++;
break;
}
}
if(!flag)
{
if(kuoh!=0)
flag++;
}
return(flag);
}
另外,站长团上有产品团购,便宜有保证

❹ C语言算24点

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

char op[3], o[5]="+-*/";

float n[4], on[10];

int used[4] = {0}, top=0, tp=0, x;

void chk(float k);

void search24(int d);

float calc(float n1, float n2, char o);

void make(int i, float p, float q, char o, int d);

int main( void )

{

printf("please input 4 card number: ");

scanf("%f%f%f%f", &n[0], &n[1], &n[2], &n[3]);

search24(0);

printf("No answer. ");

return 0;

}


void chk(float k)

{

if( (tp != 3) || ( fabs(k-24.0) > 0.000001 )) //没有用完3个运算符或者结果不为24就退出.

return;

for(x=0; x<5; x+=2) //这样设计是为了使3个选中的符号都可以得到输出.

printf("%g%c%g=%g ", on[x], op[x/2], on[x+1], //分析得到的.

calc(on[x], on[x+1], op[x/2]));

system("pause");

exit(0);

}

float calc(float n1, float n2, char o)

{

switch(o){

case '+': return (n1+n2);

case '-': return (n1-n2);

case '*': return (n1*n2);

case '/': return (n1/n2);

default: exit(0);

}

}

void make(int i, float p, float q, char o, int d)

{

if(fabs(q)>0.000001 || o!='/') //除数不为0,或者为0的时候不能为除数.

n[i] = calc(p, q, o);

op[tp++] = o;

chk(n[i]);

search24(d+1);

tp--; //因为是全是全局变量,所以在做试验性的循环递归问题时,如果失败,要在递归函数后面重新恢复回原来的值

}

void search24(int d)

{

int i, j, k;

float p, q;

if(d>=3) //控制递归深度,就是运算符的输出个数.

return;

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

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

if( (i!=j)&& (used[i]+used[j] == 0) ) //i!=j是防止重复,(used[i]+used[j] == 0)是防止又再匹配已经用过的j,

//但是i可以新来.

{

used[j] = 1; //j得到匹配之后,赋值为1,表示已经使用

p=n[i];

q=n[j];

on[top++] = p;

on[top++] = q;

for(k=0; k<4; k++) //运算符的循环试用.

make(i, p, q, o[k], d);

n[i] = p; //因为是全是全局变量,所以在做试验性的循环递归问题时,

used[j] = 0; //如果失败,要在递归函数后面重新恢复回原来的值

top -= 2; //

}

}

出处:http://blog.sina.com.cn/s/blog_491de9d60100d5er.html

阅读全文

与24点的算法c语言相关的资料

热点内容
海康摄像萤石云服务器 浏览:814
安卓手机怎么改安卓版名 浏览:147
雅思听力807词汇pdf 浏览:897
黄豆私人加密 浏览:192
java分钟转换小时 浏览:245
易语言服务器如何提高 浏览:591
网站主机服务器地址查看 浏览:859
算法学不会能当程序员吗 浏览:119
程序员技术交流研究 浏览:814
javaresponse文件 浏览:734
linuxrar压缩文件夹 浏览:218
魅蓝手机连接不上服务器怎么回事 浏览:379
工行app怎么改已绑定银行卡 浏览:533
oppo芯片程序员 浏览:602
oppok3应用怎么加密 浏览:327
电脑软盘怎么加密码 浏览:815
服务器光交换机有什么用 浏览:708
app上怎么拍蛙小侠 浏览:217
志高聊天app怎么下载 浏览:635
邮政app怎么不能扫付款码 浏览:559