?? 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 + -