当前位置:首页 » 编程语言 » 算24点c语言

算24点c语言

发布时间: 2024-08-06 15:59:41

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;
}

热点内容
python提示符 发布:2024-11-25 22:53:28 浏览:494
超低温疫苗存储冰柜生产厂家 发布:2024-11-25 22:32:58 浏览:537
x86linux 发布:2024-11-25 22:09:24 浏览:450
qq群怎么设置上传 发布:2024-11-25 22:08:37 浏览:16
加密户籍 发布:2024-11-25 22:08:32 浏览:214
newman算法 发布:2024-11-25 21:34:55 浏览:201
a算法概念 发布:2024-11-25 21:24:16 浏览:588
jquery源码书籍 发布:2024-11-25 21:19:50 浏览:804
银行卡输入密码超限怎么办 发布:2024-11-25 21:09:07 浏览:958
编译指令多发 发布:2024-11-25 20:58:17 浏览:751