【JAVA】PAT 乙级 1046 划拳

【JAVA】PAT 乙级 1046 划拳


题目链接

划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字。如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢了,输家罚一杯酒。两人同赢或两人同输则继续下一轮,直到唯一的赢家出现。

下面给出甲、乙两人的划拳记录,请你统计他们最后分别喝了多少杯酒。

输入格式:
输入第一行先给出一个正整数 N(≤100),随后 N 行,每行给出一轮划拳的记录,格式为:

甲喊 甲划 乙喊 乙划

其中喊是喊出的数字,划是划出的数字,均为不超过 100 的正整数(两只手一起划)。

输出格式:
在一行中先后输出甲、乙两人喝酒的杯数,其间以一个空格分隔。

输入样例:
5
8 10 9 12
5 10 5 10
3 8 5 12
12 18 1 13
4 16 12 15

输出样例:
1 2

数组a中的元素分别为甲喊 甲划 乙喊 乙划的数字,甲输返回-1,乙输返回1,其他情况返回0

	static int check(int[] a) {
		int sum = a[0] + a[2];
		if (sum == a[1] && sum != a[3]) {
			return 1;
		}
		if (sum == a[3] && sum != a[1]) {
			return -1;
		}
		return 0;
	}

完整代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class Main{
	public static void main(String[] args) throws Exception {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		int n = sti(bf.readLine());
		int counta = 0;
		int countb = 0;
		for (int i = 0; i < n; i++) {
			int[] abh = stii(bf.readLine().split(" "));
			int re = check(abh);
			if (re == 0) {
				continue;
			} else if (re == 1) {
				countb++;
			} else {
				counta++;
			}
		}
		out.print(counta + " " + countb);
		out.flush();
	}

	static int check(int[] a) {
		int sum = a[0] + a[2];
		if (sum == a[1] && sum != a[3]) {
			return 1;
		}
		if (sum == a[3] && sum != a[1]) {
			return -1;
		}
		return 0;
	}

	static int sti(String a) {
		return Integer.valueOf(a);
	}

	static int[] stii(String[] a) {
		int[] re = new int[a.length];
		for (int i = 0; i < re.length; i++) {
			re[i] = sti(a[i]);
		}
		return re;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值