C/C++ Programming Learning-Week 5 ④ Rock Paper Scissors

Topic link

Title description

Rock, paper, scissors is a common guessing game. Rock beats scissors, scissors beats cloth, and cloth beats rock. If two punches are the same, there is no difference.

One day, Little A and Little B happened to be playing rock paper scissors. It is known that their punches are cyclical, such as: "rock-cloth-rock-scissors-rock-cloth-rock-scissors...", which means that the cycle of "rock-cloth-rock-scissors" is constant. Circular. Excuse me, after the small A and the small B are compared with the N rounds, who has won the more rounds?

Input format The
input consists of three lines.

The first line contains three integers:
N, N A , N B , which respectively represent
the period length of a punch by the smaller A and the period length of a punch by the smaller B after N rounds. 0<N,N A ,N B <100.

The second line contains N A integers, indicating the law of small A's punches.

The third line contains N B integers, which represents the law of small B's punches.

Among them, 0 means "rock", 2 means "scissors", and 5 means "cloth". Use a single space to separate two adjacent integers.

Output format
Output one line. If Little A wins more rounds, output "A"; if Little B wins more rounds, output "B"; if the two are tied, output "draw".

Tips
For test data, the guessing process is:

A:0 2 5 0 2 5 0 2 5 0

B:0 5 0 2 0 5 0 2 0 5

A won 4 rounds, B won 2 rounds, the two sides tied for 4 rounds, so A won more rounds.

Sample Input

10 3 4
0 2 5
0 5 0 2

Sample Output

A

Ideas

To simulate the process of rock-paper-scissors, you can write a function to determine who has won.

C language code:

#include <stdio.h>
#include <math.h>
#include <string.h>
const long long N = 1e6 + 10;
long long n, m, cnt = 0, ans = 0, sum = 0;
long long is_a_win(long long a, long long b)//0石头 2剪刀 5布;//写个函数判断谁赢啦
{
    
    
	if((a == 0 && b == 2) || (a == 2 && b == 5) || (a == 5 && b == 0)) return 1;//A赢啦
	else if(a==b) return 0;//平手
	else return -1;//B赢啦
}
int main()
{
    
    
    long long a[N], b[N], A, B;
	scanf("%lld %lld %lld",&n, &A, &B);
	for(long long i = 0; i < A; i++)//录入A的规律
		scanf("%lld", &a[i]);
	for(long long i = 0; i < B; i++)//录入B的规律
		scanf("%lld", &b[i]);
    for(long long i = 0; i < n; i++)//模拟n场猜拳
    {
    
    
    	long long nowa, nowb;//这一局,第i场
    	nowa = a[i % A];//A出的是nowa
    	nowb = b[i % B];//B出的是nowb
    	cnt += is_a_win(nowa, nowb);//a赢啦,cnt就加一,B赢啦,就减一
    }
    if(cnt > 0) printf("A");//cnt>0 说明A赢得多
    else if(cnt < 0) printf("B");//cnt<0 说明B赢得多
    else printf("draw");//cnt==0, 平手    
	return 0;
}

C++ code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	int n, a, b, numa[105], numb[105];
	while(cin >> n >> a >> b)
	{
    
    
		int A = 0, B = 0;
		memset(numa, 0, sizeof(numa));
		memset(numb, 0, sizeof(numb));
		for(int i = 0; i < a; i++)
			cin >> numa[i];
		for(int i = 0; i < b; i++)
			cin >> numb[i];
		for(int i = 0; i < n; i++)
			if((numa[i % a] == 0 && numb[i % b] == 2) || (numa[i % a] == 2 && numb[i % b] == 5) || (numa[i % a] == 5 && numb[i % b] == 0)) A++;
			else if((numa[i % a] == 2 && numb[i % b] == 0) || (numa[i % a] == 5 && numb[i % b] == 2) || (numa[i % a] == 0 && numb[i % b] == 5)) B++;
		if(A > B) cout << "A" << endl;
		else if(A < B) cout << "B" << endl;
		else if(A == B) cout << "draw" << endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/112909392