北理工C语言【游戏】取石子#includestdio.h
#includeconio.h
int main(void)//也就是a或者b有一项为2或者1时,有必胜方法.
{
int i, T, a[50] = { 0 }, b[50] = { 0 }, c[50] = { 0 };
scanf("%d", T);
for (i = 0; iT;i)
{
scanf("%d", a[i]);
scanf("%d", b[i]);
if (a[i] == 1 || a[i] == 2 || b[i] == 1 || b[i] == 2)
c[i] = 1;
}
for (i = 0; iT;i)
{
if (c[i] == 1)
printf("YES\n");
else
printf("NO\n");
}
getch();
return 0;
}
求C语言取石子游戏的思路 请详细点#include stdio.h
int main ()
{
int a,b,c;
const double d=1.6180339887498948482045 ;
while(scanf("%d%d",a,b)!=EOF){
if (a==b) {printf("1\n"); continue;}
if (ab) {c=a-b;a=b;}
else c=b-a;
if (a==(int)(c*d))
printf("0\n");
else printf("1\n");
}
return 0;
}
C语言捡石子游戏可以用递归来做,
假设 有A,B两堆石子 。A的数量是x , B的是y
递归的出口是3个状态 。
1:其一等于1,另一个等于2(输)
2:其一等于1,另一个2(赢)
3:其一等于2,另一个1(赢)
另外,只需要定义操作了 , 操作只能是两者之一 。其一:(de_both)两堆都减去同一数字的石子 。另外一个(de_one)就是人选一堆,拿掉任意个数的石子 。
递归过程如下;
voidsimulate(int a,int b)
{
switch(state)
{
case 1:
you lose;
case 2:
break;
case 3:
you win;
}
if(abs(a,b)=1)/*这时候一定能赢*/
{
de_both(min(a,b)-1); /*两边都取走两者中最小数-1个石子,形成状态1的形式*/
}
else
{
de_one(random);/*这里的random只需要不使两者之差=1即可*/
}
simulate(a,b);
}
C语言编写 取苹果游戏这是我变得一个类似程序,你改改就行了 。
Nim游戏是一个著名而且有很多版本的经典游戏,其获胜策略是
两名参与者交替从一堆石子中取出若干数目其数目有参与者决定 。但
是要求参与者每次至少取出一个,至多一半,然后由另一名参与者继
续取,拿到最后一颗石子者输 。人和电脑随几分先后,电脑有聪明和愚蠢模式
共四种情况
#includeiostream
#includestring
#includecstdlib
#includectime
#includecmath
using namespace std;
/*设置种子*/
void rand_seed( )
{
int seed=static_castint(time(0));
srand(seed);
}
/*随机数函数*/
int rand_int(int a,int b)
{
return a rand( )%(b-a 1);
}
/*检测电脑是否输了*/
int checkwin1(int stoneNum)
{
int flag;
if(stoneNum==0)
{
cout"Congelations!You win the game!\n";
flag=0;
}
else
{
cout"It is your turn!Please input your taking stones: \n";
flag=1;
}
return flag;
}
/*检测人是否输了*/
int checkwin2(int stoneNum)
{
int flag;
if(stoneNum==0)
{
cout"Sorry!You lost the game!\n";
flag=0;
}
else
{
cout"It is computer's turn! \n";
flag=1;
}
return flag;
}
/*主函数*/
int main( )
{
int i,j,s,flag;
int stoneNum;
int order,take,pattern,compTake,peoTake;
cout"********************** Welcom To Use ************************\n";
cout"===================== The Game Of Nim =======================\n";
cout"\n";
cout"Nim游戏是一个著名而且有很多版本的经典游戏,其获胜策略是\n";
cout"两名参与者交替从一堆石子中取出若干数目其数目有参与者决定 。但\n";
cout"是要求参与者每次至少取出一个,至多一半,然后由另一名参与者继\n";
cout"续?。玫阶詈笠豢攀诱呤?。\n";
cout"\n";
cout"============================================================\n";
cout"===============================================sunjing024制作=====\n";
rand_seed( );
stoneNum=rand_int(10,100);
cout"There are total "stoneNum" stones!\n";
order=rand_int(0,1);
pattern=rand_int(0,1);
if(order==0pattern==0)s=0;
else if(order==0pattern==1)s=1;
else if(order==1pattern==0)s=2;
else if(order==1pattern==1)s=3;
switch(s)
{
case 0:
cout"Computer takes first and he choose clever pattern!\n";
for(i=1;stoneNum!=0i60flag!=0;i)
{
for(j=1;j=7take=stoneNum/2;j)
{
if(stoneNum==pow(2,j)-1)
flag=1;
}
if(stoneNum==1)
{
stoneNum=0;
compTake=1;
}
else if(flag==1)
{
compTake=rand_int(1,stoneNum/2);
cout"Computer takes "compTake" stones!\n";
stoneNum=stoneNum-compTake;
cout"There are "stoneNum" stones left!\n";
}
else
{
for(take=1;take=stoneNum/2;take)
{
for(j=1;j=7;j)
{
if(stoneNum-take==pow(2,j)-1take=stoneNum/2)
{
cout"Computer takes "take" stones!\n";
stoneNum=stoneNum-take;
cout"There are "stoneNum" stones left!\n";
goto loop;
}
}
}
}
loop: compTake=take;
flag=checkwin1(stoneNum);
cinpeoTake;
cout"You take "peoTake" stones!\n";
stoneNum=stoneNum-peoTake;
cout"There are "stoneNum" stones left!\n";
flag=checkwin2(stoneNum);
}
break;
case 1:
cout"Computer takes first and he choose stupy pattern!\n";
for(i=1;stoneNum!=0i60flag!=0;i)
{
if(stoneNum==1)
{
cout"computer takes 1 stones!\n";
stoneNum=0;
}
else
{
compTake=rand_int(1,stoneNum/2);
cout"Computer takes "compTake" stones!\n";
stoneNum=stoneNum-compTake;
cout"There are "stoneNum" stones left!\n";
}
flag=checkwin1(stoneNum);
cinpeoTake;
cout"You take "peoTake"stones!\n";
stoneNum=stoneNum-peoTake;
cout"There are "stoneNum"stones left!\n";
flag=checkwin2(stoneNum);
}
break;
case 2:
cout"You takes first and computer choose clever pattern!\n";
for(i=1;stoneNum!=0i60flag!=0;i)
{
cout"Please input your taking stones: \n";
cinpeoTake;
cout"You take "peoTake" stones!\n";
stoneNum=stoneNum-peoTake;
cout"There are "stoneNum" stones left!\n";
flag=checkwin2(stoneNum);
for(j=1;j=7take=stoneNum/2;j)
{
if(stoneNum==pow(2,j)-1)
flag=1;
}
if(stoneNum==1)
{
stoneNum=0;
compTake=1;
}
else if(flag==1)
{
compTake=rand_int(1,stoneNum/2);
cout"Computer takes "compTake" stones!\n";
stoneNum=stoneNum-compTake;
cout"There are "stoneNum" stones left!\n";
}
else
{
for(take=1;take=stoneNum/2;take)
{
for(j=1;j=7;j)
{
if(stoneNum-take==pow(2,j)-1take=stoneNum/2)
{
cout"Computer takes "take" stones!\n";
stoneNum=stoneNum-take;
cout"There are "stoneNum" stones left!\n";
goto loop1;
}
}
}
}
loop1: compTake=take;
flag=checkwin1(stoneNum);
}
break;
case 3:
cout"You takes first and computer choose stupy pattern!\n";
for(i=1;stoneNum!=0i60flag!=0;i)
{
cout"Please input your taking stones: \n";
cinpeoTake;
cout"You take "peoTake" stones!\n";
stoneNum=stoneNum-peoTake;
cout"There are "stoneNum" stones left!\n";
flag=checkwin2(stoneNum);
if(stoneNum==1)
{
cout"computer takes 1 stones!\n";
stoneNum=0;
}
else
{
compTake=rand_int(1,stoneNum/2);
cout"Computer takes "compTake" stones!\n";
stoneNum=stoneNum-compTake;
cout"There are "stoneNum" stones left!\n";
}
flag=checkwin1(stoneNum);
}
break;
}
cout"The end!Welcome to use again!\n";
return 0;
}
c语言 取石子游戏推荐,为免你不熟悉c,特意改了一份可以在vc6.0编译通过的c语言源代码:
#include stdio.h
typedef __int64 LL;
LL SG(LL x)
{
return x % 2 == 0 ? x / 2 : SG(x / 2);
}
int main()
{
int T;
scanf("%d",T);
while(T--)
{
int n;
LL ans = 0;
scanf("%d",n);
while(n--)
{
LL x;
scanf("%I64d",x);
ans ^= SG(x);
}
if(ans)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
【c语言函数取石子 c语言取子串】关于c语言函数取石子和c语言取子串的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- mysql数据库查询题库,mysql查询例题
- 饥荒游戏下载,饥荒下载破解版全人物
- vb.net闪烁效果 c# 闪烁
- 快手如何做精准推广卖货,快手如何推广产品
- 灯饰营销如何,灯饰销售渠道
- 角色扮演校园游戏,角色扮演游戏小游戏
- 关于显示系统名命令linux的信息
- 电脑cpu什么型号值钱,电脑cpu哪种好
- 超薄台式电脑电源怎么接,超薄台式电脑电源怎么接线