PAT-BASIC1018——锤子剪刀布

我的PAT-BASIC代码仓:https://github.com/617076674/PAT-BASIC

原题链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805304020025344

题目描述:

知识点:if-else语句

思路:多用几个if-else语句讨论清楚各种结果对应的输出

时间复杂度是O(n),其中n为输入的交锋次数。空间复杂度是O(1)。

C++代码:

#include<iostream>

using namespace std;

int main() {
	int count;
	cin >> count;
	int win1 = 0;
	int tie1 = 0;
	int lose1 = 0;
	int win2 = 0;
	int tie2 = 0;
	int lose2 = 0;

	int win1C = 0;
	int win1J = 0;
	int win1B = 0;

	int win2C = 0;
	int win2J = 0;
	int win2B = 0;
	for (int i = 0; i < count; i++) {
		char letter1;
		char letter2;
		cin >> letter1 >> letter2;
		if (letter1 == 'C') {
			if (letter2 == 'C') {
				tie1++;
				tie2++;
			} else if (letter2 == 'J') {
				win1++;
				lose2++;
				win1C++;
			} else if (letter2 == 'B') {
				lose1++;
				win2++;
				win2B++;
			}
		} else if (letter1 == 'J') {
			if (letter2 == 'C') {
				lose1++;
				win2++;
				win2C++;
			} else if (letter2 == 'J') {
				tie1++;
				tie2++;
			} else if (letter2 == 'B') {
				win1++;
				lose2++;
				win1J++;
			}
		} else if (letter1 == 'B') {
			if (letter2 == 'C') {
				win1++;
				lose2++;
				win1B++;
			} else if (letter2 == 'J') {
				lose1++;
				win2++;
				win2J++;
			} else if (letter2 == 'B') {
				tie1++;
				tie2++;
			}
		}
	}
	cout << win1 << " " << tie1 << " " << lose1 << endl;
	cout << win2 << " " << tie2 << " " << lose2 << endl;

	if (win1B > win1C) {
		if (win1J > win1B) {
			cout << "J ";
		} else if (win1J == win1B) {
			cout << "B ";
		} else if (win1J < win1B) {
			cout << "B ";
		}
	} else if (win1B == win1C) {
		if (win1J > win1B) {
			cout << "J ";
		} else if (win1J == win1B) {
			cout << "B ";
		} else if (win1J < win1B) {
			cout << "B ";
		}
	} else if (win1B < win1C) {
		if (win1J > win1C) {
			cout << "J ";
		} else if (win1J == win1C) {
			cout << "C ";
		} else if (win1J < win1C) {
			cout << "C ";
		}
	}

	if (win2B > win2C) {
		if (win2J > win2B) {
			cout << "J";
		} else if (win2J == win2B) {
			cout << "B";
		} else if (win2J < win2B) {
			cout << "B";
		}
	} else if (win2B == win2C) {
		if (win2J > win2B) {
			cout << "J";
		} else if (win2J == win2B) {
			cout << "B";
		} else if (win2J < win2B) {
			cout << "B";
		}
	} else if (win2B < win2C) {
		if (win2J > win2C) {
			cout << "J";
		} else if (win2J == win2C) {
			cout << "C";
		} else if (win2J < win2C) {
			cout << "C";
		}
	}

	return 0;
}

C++解题报告:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值