火柴题解
来源:互联网 发布:淘宝店铺的模板好卖吗 编辑:程序博客网 时间:2024/04/28 05:06
// huocai.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<string>
#define NUM 20
using namespace std;
void main()
{
//规则的输出
cout<<endl;
cout<<"***************************************************"<<endl;
cout<<"游戏规则:共有67根火柴,2个人依次拿取,每个人每次只"<<endl;
cout<<"能拿1跟或者2跟或者3跟火柴。拿到最后一跟火柴的人算输"<<endl;
cout<<"****************************************************"<<endl;
cout<<endl;
//变量的定义
int match_num=67; //火柴数目
int *p_match_num; //指向火柴数目的指针
int player = 0; //玩家变量,偶数代表玩家1,奇数代表玩家2
char player_first[NUM]; //玩家1的名字
char player_second[NUM]; //玩家2的名字
char judge; //判断变量,值为'y'时,表示玩家1先开局
int put_num=0; //拿走火柴的数目
char temp[NUM];
char *p_player_now;
int *p_put_num; //指向拿走火柴数目的指针
int *p_player;
//指针赋值
p_match_num=&match_num;
p_put_num=&put_num;
p_player=&player;
p_player_now=player_first;
//函数的定义
void match(int* p_player, int* p_match_num,int* p_put_num,char* p_player_now); //火柴拿走数目函数
//游戏开始
cout<<"请输入玩家1的名字:";
gets(player_first);
cout<<"请输入玩家2的名字:";
gets(player_second);
//玩家开局顺序的选择
cout<<"玩家“"<<player_first<<"”选择拿火柴顺序(“F(First)/S(Second)”)";
cin>>judge;
if(judge=='s')1
{
strcpy(temp,player_first);
strcpy(player_first,player_second);
strcpy(player_second,temp);
}
cout<<"玩家“"<<player_first<<"”先开始"<<endl;
cout<<endl;
//火柴的拿取
while(1)
{
if(player%2==0)
p_player_now=player_first;
else
p_player_now=player_second;
match(p_player,p_match_num,p_put_num,p_player_now);
match_num=match_num-put_num;
if(match_num==0)
{
cout<<"玩家“"<<p_player_now<<"”您输了!"<<endl;
break;
}
}
}
void match(int* p_player,int* p_match_num,int* p_put_num,char* p_player_now)
{
cout<<"请“"<<p_player_now<<"”输入准备拿走火柴的数目:";
cin>>*p_put_num;
while(*p_put_num!=1 && *p_put_num!=2 && *p_put_num!=3 || *p_match_num-*p_put_num<0)
{
if(*p_put_num!=1 && *p_put_num!=2 && *p_put_num!=3)
{
cout<<"您输入的数值不合法,拿走的火柴数目只能是 1 或 2 或 3,请重新输入:";
cin>>*p_put_num;
}
if(*p_match_num-*p_put_num<0)
{
cout<<"您输入的数值大于剩余火柴数目,请重新输入:";
cin>>*p_put_num;
}
}
cout<<"您拿走的火柴数目是: "<<*p_put_num<<endl;
cout<<"剩余火柴数目是: "<<*p_match_num-*p_put_num<<endl;
cout<<endl;
(*p_player)++;
}
//
#include "stdafx.h"
#include<iostream>
#include<string>
#define NUM 20
using namespace std;
void main()
{
//规则的输出
cout<<endl;
cout<<"***************************************************"<<endl;
cout<<"游戏规则:共有67根火柴,2个人依次拿取,每个人每次只"<<endl;
cout<<"能拿1跟或者2跟或者3跟火柴。拿到最后一跟火柴的人算输"<<endl;
cout<<"****************************************************"<<endl;
cout<<endl;
//变量的定义
int match_num=67; //火柴数目
int *p_match_num; //指向火柴数目的指针
int player = 0; //玩家变量,偶数代表玩家1,奇数代表玩家2
char player_first[NUM]; //玩家1的名字
char player_second[NUM]; //玩家2的名字
char judge; //判断变量,值为'y'时,表示玩家1先开局
int put_num=0; //拿走火柴的数目
char temp[NUM];
char *p_player_now;
int *p_put_num; //指向拿走火柴数目的指针
int *p_player;
//指针赋值
p_match_num=&match_num;
p_put_num=&put_num;
p_player=&player;
p_player_now=player_first;
//函数的定义
void match(int* p_player, int* p_match_num,int* p_put_num,char* p_player_now); //火柴拿走数目函数
//游戏开始
cout<<"请输入玩家1的名字:";
gets(player_first);
cout<<"请输入玩家2的名字:";
gets(player_second);
//玩家开局顺序的选择
cout<<"玩家“"<<player_first<<"”选择拿火柴顺序(“F(First)/S(Second)”)";
cin>>judge;
if(judge=='s')1
{
strcpy(temp,player_first);
strcpy(player_first,player_second);
strcpy(player_second,temp);
}
cout<<"玩家“"<<player_first<<"”先开始"<<endl;
cout<<endl;
//火柴的拿取
while(1)
{
if(player%2==0)
p_player_now=player_first;
else
p_player_now=player_second;
match(p_player,p_match_num,p_put_num,p_player_now);
match_num=match_num-put_num;
if(match_num==0)
{
cout<<"玩家“"<<p_player_now<<"”您输了!"<<endl;
break;
}
}
}
void match(int* p_player,int* p_match_num,int* p_put_num,char* p_player_now)
{
cout<<"请“"<<p_player_now<<"”输入准备拿走火柴的数目:";
cin>>*p_put_num;
while(*p_put_num!=1 && *p_put_num!=2 && *p_put_num!=3 || *p_match_num-*p_put_num<0)
{
if(*p_put_num!=1 && *p_put_num!=2 && *p_put_num!=3)
{
cout<<"您输入的数值不合法,拿走的火柴数目只能是 1 或 2 或 3,请重新输入:";
cin>>*p_put_num;
}
if(*p_match_num-*p_put_num<0)
{
cout<<"您输入的数值大于剩余火柴数目,请重新输入:";
cin>>*p_put_num;
}
}
cout<<"您拿走的火柴数目是: "<<*p_put_num<<endl;
cout<<"剩余火柴数目是: "<<*p_match_num-*p_put_num<<endl;
cout<<endl;
(*p_player)++;
}
- 火柴题解
- Noip2013火柴排队题解
- [noip2013]火柴排队 题解
- noip2008T2火柴棒等式题解
- 火柴排队(codevs 3286)题解
- 【题解】洛谷1149 火柴棒等式
- NOIp 2013 提高组 火柴排队 题解
- 【NOIP2008】T2 火柴棒等式 题解
- 【基础练习】【搜索/枚举】codevs1168 火柴棒等式题解
- 【基础练习】【离散化+逆序对】codevs3286 火柴排队题解
- NOIP 2013 火柴排队 题解(离散化+树状数组)
- 玩火柴
- 火柴问题
- 火柴游戏
- 火柴排队
- 取火柴
- 火柴天堂
- 火柴排队
- 编程开始
- base-kernel-实现策略
- 修改MyEclipse8.5的启动画面
- 风姿花传歌词
- C++ 简单文件读写
- 火柴题解
- 动态代理
- 枚举并隐藏窗口函数[汇编语言]
- 如何花两年的时间去面试一个人
- 动态规划——LCS问题
- 网上笔仗个人见解
- TRAP 收到信号后不立即执行的问题
- 新手如何成功搭建Android开发环境
- Fiddler相关