Skip to content

Latest commit

 

History

History
99 lines (80 loc) · 2.01 KB

石头剪刀布.md

File metadata and controls

99 lines (80 loc) · 2.01 KB

问题描述

甲乙两人,石头剪刀布,C,J,B,统计出,二人的胜平负次数,以及胜出最多的手势,如多有多个,输出字母值最小的那个

代码

#include<stdio.h>

// 将字符B、C、J转为0,1,2
int changetoNum(char ch) {
	if (ch == 'B')
	{
		return 0;
	}

	if (ch == 'C')
	{
		return 1;
	}

	if (ch == 'J')
	{
		return 2;
	}
	
}

int main() {

	char mp[3] = {'B', 'C', 'J'};
	int N;
	scanf("%d", &N);
	
	// 记录甲,乙的胜平负次数
	int a_res[3] = { 0 };
	int b_res[3] = { 0 };
	
	// 记录甲乙二人胜时的手势次数
	int a_hand[3] = { 0 };
	int b_hand[3] = { 0 };

	int c1, c2;
	int k1, k2;

	for (int i = 0; i < N; i++)
	{
		getchar();
		scanf("%c %c", &c1, &c2);
		k1 = changetoNum(c1);
		k2 = changetoNum(c2);

		// 甲胜
		if ((k1 + 1) % 3 == k2)
		{
			a_res[0]++;
			b_res[2]++;
			a_hand[k1]++;
		// 甲乙平
		} else if (k1 == k2)
		{
			a_res[1]++;
			b_res[1]++;
		// 乙胜
		} else if ((k2 + 1) % 3 == k1)
		{
			a_res[2]++;
			b_res[0]++;
			b_hand[k2]++;
		}
	}
	
	printf("%d %d %d\n", a_res[0], a_res[1], a_res[2]);
	printf("%d %d %d\n", b_res[0], b_res[1], b_res[2]);

	int id1 = 0;
	int id2 = 0;
	for (int i = 1; i < 3; i++)
	{
		if (a_hand[i] > a_hand[id1])
		{
			id1 = i;
		}

		if (b_hand[i] > b_hand[id2])
		{
			id2 = i;
		}
	}

	printf("%c %c\n", mp[id1], mp[id2]);
	

	return 0;
}

注意事项

  1. 要求输出字典最小的解,因此将三种手势按字典排序,此外,B,C,J是一个循环相克顺序,即B胜C,C胜J,J胜B,因此将B,C,J对应为0,1,2,可以用一个字符数组来处理
  2. 对于甲乙输入的手势c1,c2,通过changetoNum函数,转换为k1,k2,因此,如若甲想赢,必须有,(k1 + 1) % 3 == k2; 同理二人平局有k1==k2;最后如若乙想赢,则有(k2 + 1) % 3 == k1
  3. 在获取甲乙获胜次数最多的手势时,利用id1,id2,遍历记录3种手势的获胜次数的数组,将最优结果的下标返回给id1,id2,通过一一对应字符数组,获取相应的手势