C++版本 08:石头剪刀布

33 篇文章 2 订阅
23 篇文章 1 订阅

描述

石头剪刀布是常见的猜拳游戏。石头胜剪刀,剪刀胜布,布胜石头。如果两个人出拳一样,则不分胜负。

一天,小A和小B正好在玩石头剪刀布。已知他们的出拳都是有周期性规律的,比如:“石头-布-石头-剪刀-石头-布-石头-剪刀……”,就是以“石头-布-石头-剪刀”为周期不断循环的。请问,小A和小B比了N轮之后,谁赢的轮数多?

输入

输入包含三行。
第一行包含三个整数:N,NA,NB,分别表示比了N轮,小A出拳的周期长度,小B出拳的周期长度。0 < N,NA,NB < 100。
第二行包含NA个整数,表示小A出拳的规律。
第三行包含NB个整数,表示小B出拳的规律。
其中,0表示“石头”,2表示“剪刀”,5表示“布”。相邻两个整数之间用单个空格隔开。

输出

输出一行,如果小A赢的轮数多,输出A;如果小B赢的轮数多,输出B;如果两人打平,输出draw。

样例输入

10 3 4
0 2 5
0 5 0 2

样例输出

A

提示

对于测试数据,猜拳过程为:
A:0 2 5 0 2 5 0 2 5 0
B:0 5 0 2 0 5 0 2 0 5
A赢了4轮,B赢了2轮,双方打平4轮,所以A赢的轮数多。
#include <stdio.h>
int a[101],b[101],n,i,c,d,s=0,v=0;
int main()
{
	scanf("%d %d %d",&n,&c,&d);
	for(i=1;i<=c;i++)
		scanf("%d",&a[i]);
	for(i=1;i<=d;i++)
		scanf("%d",&b[i]);
	for(i=c+1;i<=n;i++)
		a[i]=a[i-c];
	for(i=d+1;i<=n;i++)
		b[i]=b[i-d];
	for(i=1;i<=n;i++)
	{
		if(a[i]==0&&b[i]==2||a[i]==2&&b[i]==5||a[i]==5&&b[i]==0)
			s++;
		if(b[i]==0&&a[i]==2||b[i]==2&&a[i]==5||b[i]==5&&a[i]==0)
			v++; 
	}
	if(s>v)
	 	printf("A");
	if(v>s) 
		printf("B");
	if(s==v) 
		printf("draw");
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,可以的!我可以为您提供一个基本的类实现石头剪刀布游戏的示例代码,如下所示: ```c++ #include <iostream> #include <ctime> using namespace std; class RockPaperScissors { private: int playerScore, computerScore, tieScore; // 0: 石头, 1: 剪刀, 2: 布 int playerChoice, computerChoice; string choices[3] = {"石头", "剪刀", "布"}; public: RockPaperScissors() { playerScore = 0; computerScore = 0; tieScore = 0; } void play() { while (true) { cout << "请出拳: (0)石头 (1)剪刀 (2)布" << endl; cin >> playerChoice; if (playerChoice < 0 || playerChoice > 2) { cout << "输入无效,请重新输入!" << endl; continue; } computerChoice = rand() % 3; cout << "电脑出拳: " << choices[computerChoice] << endl; int result = (playerChoice - computerChoice + 3) % 3; if (result == 0) { cout << "平局!" << endl; tieScore++; } else if (result == 1) { cout << "你赢了!" << endl; playerScore++; } else { cout << "你输了!" << endl; computerScore++; } cout << "当前比分: " << playerScore << "-" << computerScore << "-" << tieScore << endl; char again; cout << "是否再来一局?(Y/N)" << endl; cin >> again; if (again != 'Y' && again != 'y') { break; } } } }; int main() { srand(time(NULL)); RockPaperScissors game; game.play(); return 0; } ``` 这个类实现了一个简单的石头剪刀布游戏,玩家通过在控制台输入数字表示出拳,电脑随机生成出拳结果,然后判断胜负,并记录比分。玩家可以选择是否再来一局。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值