導航:首頁 > 源碼編譯 > 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語言相關的資料

熱點內容
javaresponse文件 瀏覽:732
linuxrar壓縮文件夾 瀏覽:216
魅藍手機連接不上伺服器怎麼回事 瀏覽:377
工行app怎麼改已綁定銀行卡 瀏覽:531
oppo晶元程序員 瀏覽:600
oppok3應用怎麼加密 瀏覽:325
電腦軟盤怎麼加密碼 瀏覽:813
伺服器光交換機有什麼用 瀏覽:706
app上怎麼拍蛙小俠 瀏覽:215
志高聊天app怎麼下載 瀏覽:633
郵政app怎麼不能掃付款碼 瀏覽:557
筆記本電腦雙理由配置命令 瀏覽:63
拿著文件夾怎麼畫 瀏覽:875
博瑞發動機壓縮比 瀏覽:730
linux開源郵件伺服器 瀏覽:777
ios打地鼠源碼 瀏覽:472
伺服器換主機需要什麼系統 瀏覽:749
linux監控jvm內存 瀏覽:79
空調壓縮機自製工具 瀏覽:518
命令行控制路由器 瀏覽:957