📄 playingcards.cpp
字号:
/*有一定数量total的扑克牌,现在两个人甲乙用这些扑克牌玩一个游戏,两人轮流从
扑克牌中为放回地拿出几张扑克牌,每次最多拿max张,怎样拿才能保证第一个拿牌
的人获胜,本程序提供了一种方法,程序默认甲是第一个拿牌的人*/
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
class PlayingCards
{
private:
int total,max;
public:
PlayingCards(int total,int max)
{
this->total=total;
this->max=max;
}
void run()
{
//甲先拿
int temp=total;
int first=(temp-max-2)%(max+1);
temp=temp-first;
cout<<"甲先拿了"<<first<<"张"<<endl;
if(temp==1)
{
cout<<"甲获得了胜利"<<endl;
}
else
{
for(;;)
{
int ran=rand()%(max)+1;
temp=temp-ran;
cout<<"接着乙拿了"<<ran<<"张"<<endl;
if(temp==1)
{
cout<<"乙拿过后只剩下1张,乙获得了胜利"<<endl;
break;
}
else
{
temp=temp-max-1+ran;
cout<<"然后甲拿了"<<max+1-ran<<"张"<<endl;
if(temp==1)
{
cout<<"甲拿过后只剩下1张,甲获得了胜利"<<endl;
break;
}
}
}
}
}
};
void main()
{
int total,max;
cout<<"请依次输入总的扑克牌数和每次允许拿的最大数目:"<<endl;
cin>>total>>max;
PlayingCards pc(total,max);
pc.run();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -