算24點c語言
A. 用c語言編寫,關於24點的程序
#include<stdio.h>
double fun(double a1,double a2,int b)
{switch(b)
{case 0:return (a1+a2);
case 1:return (a1-a2);
case 2:return (a1*a2);
case 3:return (a1/a2);
}
}
void main()
{int i,j,k,l,n,m,r,save[4];
double num[4]={1,1,1,1},tem1,tem2,tem3,abc=1111;
char sign[5]="+-*/";
printf("input 4 numbers:");
for(i=0;i<4;i++)
{scanf("%lf",num+i); save[i]=num[i];}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(j!=i)
{for(k=0;k<4;k++)
if(k!=i&&k!=j)
{for(l=0;l<4;l++)
if(l!=i&&l!=j&&l!=k)
{for(n=0;n<4;n++)
for(m=0;m<4;m++)
for(r=0;r<4;r++)
{tem1=fun(num[i],num[j],n);
tem2=fun(tem1,num[k],m);
tem3=fun(tem2,num[l],r);
if(tem3==24.0)printf("{(%d%c%d)%c%d}%c%d=24\n",save[i],sign[n],save[j],sign[m],save[k],sign[r],save[l]);
else if(tem3==-24.0)printf("{%d%c(%d%c%d)}%c%d=24\n",save[k],sign[m],save[i],sign[n],save[j],sign[r],save[l]);
else if(tem3==1.0/24.0)printf("%d%c{(%d%c%d)%c%d}=24\n",save[l],sign[r],save[i],sign[n],save[j],sign[m],save[k]);
else if(tem3==-1.0/24.0)printf("%d%c{%d%c(%d%c%d)}=24\n",save[l],sign[r],save[k],sign[n],save[i],sign[m],save[j]);
else
{tem1=fun(num[i],num[j],n);
tem2=fun(num[k],num[l],r);
tem3=fun(tem1,tem2,m);
if(tem3==24.0) printf("(%d%c%d)%c(%d%c%d)=24\n",save[i],sign[n],save[j],sign[m],save[k],sign[r],save[l]);
}
}
}
}
}
}
B. 編程C語言 24點游戲
{
printf("%-5d%-8s%-6s%-13s%-15s%-15s\n",per[i-1].score,per[i-1].name,per[i-1].age,per[i-1].num,per[i-1].adds,per[i-1].email);
if(i>1&&i%10==0)
{
printf("\t-----------------------------------\n");
printf("\t");
system("pause");
printf("\t-----------------------------------\n");
}
}
C. 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;}
D. 用C語言快算24,從1到13任意選四個數字,通過加減乘除算得結果為24,數
#include<stdio.h>
double fun(double a1,double a2,int b) //加減乘除的函數
{switch(b)
{case 0:return (a1+a2);
case 1:return (a1-a2);
case 2:return (a1*a2);
case 3:return (a1/a2);
}
}
void main()
{int i,j,k,l,n,m,r,save[4],flg=1;
double num[4]={1,1,1,1},tem1,tem2,tem3,abc=1111;
char sign[5]="+-*/";
printf("輸入四個數:");
for(i=0;i<4;i++) //判斷四個數是否滿足24點運算
{scanf("%lf",num+i); save[i]=num[i];if(save[i]>13)flg=0;}
if(flg) {
flg=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(j!=i)
{for(k=0;k<4;k++)
if(k!=i&&k!=j)
{for(l=0;l<4;l++)
if(l!=i&&l!=j&&l!=k)
{for(n=0;n<4;n++)
for(m=0;m<4;m++)
for(r=0;r<4;r++)
{tem1=fun(num[i],num[j],n);
tem2=fun(tem1,num[k],m);
tem3=fun(tem2,num[l],r);
if(tem3==24.0) {printf("{(%d%c%d)%c%d}%c%d=24\n",save[i],sign[n],save[j],sign[m],save[k],sign[r],save[l]);return;}
else if(tem3==-24.0) {printf("{%d%c(%d%c%d)}%c%d=24\n",save[k],sign[m],save[i],sign[n],save[j],sign[r],save[l]);return;}
else if(tem3==1.0/24.0) {printf("%d%c{(%d%c%d)%c%d}=24\n",save[l],sign[r],save[i],sign[n],save[j],sign[m],save[k]);return;}
else if(tem3==-1.0/24.0) {printf("%d%c{%d%c(%d%c%d)}=24\n",save[l],sign[r],save[k],sign[n],save[i],sign[m],save[j]);return;}
else
{tem1=fun(num[i],num[j],n);
tem2=fun(num[k],num[l],r);
tem3=fun(tem1,tem2,m);
if(tem3==24.0) {printf("(%d%c%d)%c(%d%c%d)=24\n",save[i],sign[n],save[j],sign[m],save[k],sign[r],save[l]);return;}
}
}
}
}
}
}
if(!flg) printf("
NO
ANSWER\n");
}
E. 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
F. C語言程序 算24點
#include<iostream>
using namespace std;
struct operation
{
int x;
int y;
int o;
}op[3];
bool co=false;
void g(int a[],int b[],int n,int i,int j)
{
int x,y;
for(x=0,y=1;x<n;x++)
{
if(x==i||x==j)continue;
b[y++]=a[x];
}
op[4-n].x=a[i];
op[4-n].y=a[j];
}
void f(int a[],int n)
{
int i,j,t,b[4];
if(n==1&&a[0]==24)co=true;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
for(t=1;t<=6;t++)
{
switch(t)
{
case 1:g(a,b,n,i,j);
b[0]=a[i]+a[j];
op[4-n].o=t;
f(b,n-1);
if(co)return;
break;
case 2:g(a,b,n,i,j);
b[0]=a[i]-a[j];
op[4-n].o=t;
f(b,n-1);
if(co)return;
break;
case 3:g(a,b,n,i,j);
b[0]=a[i]*a[j];
op[4-n].o=t;
f(b,n-1);
if(co)return;
break;
case 4:
if(a[j]!=0&&a[i]%a[j]==0)
{
g(a,b,n,i,j);
b[0]=a[i]/a[j];
op[4-n].o=t;
f(b,n-1);
if(co)return;
}
break;
case 5:g(a,b,n,i,j);
b[0]=a[j]-a[i];
op[4-n].o=t;
f(b,n-1);
if(co)return;
break;
case 6:
if(a[i]!=0&&a[j]%a[i]==0&&a[j]>a[i])
{
g(a,b,n,i,j);
b[0]=a[j]/a[i];
op[4-n].o=t;
f(b,n-1);
if(co)return;
}
break;
}
}
}
}
}
void h(int a[],int n,int x,int y,int h)
{
int i,j,b[4];
bool bx=false,by=false;
for(i=0,j=0;i<n;i++)
{
if(a[i]==x&&bx==false)
{
bx=true;
continue;
}
if(a[i]==y&&by==false)
{
by=true;
continue;
}
b[j++]=a[i];
}
for(i=0;i<n-1;i++)a[i+1]=b[i];
a[0]=h;
}
void main()
{
int a[4],i,j;
cout<<"輸入4個整數:";
for(i=0;i<4;i++)cin>>a[i];
f(a,4);
if(co)
{
for(i=0;i<3;i++)
{
for(j=0;j<4-i;j++)cout<<a[j]<<" ";
cout<<"\n運算 ";
switch(op[i].o)
{
case 1:cout<<op[i].x<<"+"<<op[i].y<<endl;
h(a,4-i,op[i].x,op[i].y,op[i].x+op[i].y);
break;
case 2:cout<<op[i].x<<"-"<<op[i].y<<endl;
h(a,4-i,op[i].x,op[i].y,op[i].x-op[i].y);
break;
case 3:cout<<op[i].x<<"*"<<op[i].y<<endl;
h(a,4-i,op[i].x,op[i].y,op[i].x*op[i].y);
break;
case 4:cout<<op[i].x<<"/"<<op[i].y<<endl;
h(a,4-i,op[i].x,op[i].y,op[i].x/op[i].y);
break;
case 5:cout<<op[i].y<<"-"<<op[i].x<<endl;
h(a,4-i,op[i].x,op[i].y,op[i].y-op[i].x);
break;
case 6:cout<<op[i].y<<"/"<<op[i].x<<endl;
h(a,4-i,op[i].x,op[i].y,op[i].y/op[i].x);
break;
}
}
cout<<"24"<<endl;
}
else cout<<"不能算出24"<<endl;
}
G. 設計C語言程序 計算24點題目要求在輸入4個數後,程序對這個4個數進行運算,若能計算出結果等於24,輸出運算
#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 = 0; i < 3; i++ )
for ( j = i+1; j < 3; j++ )
{
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,nd; dd[0] = a;
dd[1] = b;
dd[2] = c;
dd[3] = d;
for ( i = 0; i < 4; i++ )
for ( j = i+1; j < 4; j++ )
{
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;
}