美文网首页
PAT-B 1046. 划拳(15)

PAT-B 1046. 划拳(15)

作者: Rush的博客 | 来源:发表于2016-10-09 15:57 被阅读418次

传送门

https://www.patest.cn/contests/pat-b-practise/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

分析

这道题难度不大,每次输入后判断下是否相等就好了,注意:甲赢的条件是在乙没赢的情况下,同理乙赢也是一样。

源代码

//C/C++实现
#include <iostream>

using namespace std;

int main(){
    int n;
    scanf("%d", &n);
    int jiahan, jiahua, yihan, yihua, jiaying = 0, yiying = 0;
    for(int i = 0; i < n; ++i){
        scanf("%d %d %d %d", &jiahan, &jiahua, &yihan, &yihua);
        int sum = jiahan + yihan;
        if(jiahua == sum && yihua != sum){
            ++jiaying;
        }
        else if(jiahua != sum && yihua == sum){
            ++yiying;
        }
    }
    printf("%d %d\n", yiying, jiaying);
    return 0;
}
//Java实现
import java.util.Scanner;

public class Main{

    public static void main(String []args){
        Scanner scanner = new Scanner(System.in);
        int round = scanner.nextInt();
        int Awin = 0, Bwin = 0;
        if(round > 0 && round <=100){
            for(int i=0;i<round;i++){
                int Acall = scanner.nextInt();
                if(!validate(Acall))
                    System.exit(0);
                int Agesture = scanner.nextInt();
                if(!validate(Agesture))
                    System.exit(0);
                int Bcall = scanner.nextInt();
                if(!validate(Bcall))
                    System.exit(0);
                int Bgesture = scanner.nextInt();
                if(!validate(Bgesture))
                    System.exit(0);
                if((Agesture == Acall + Bcall) && (Bgesture != Acall + Bcall)){
                    Awin ++;
                }
                else if((Bgesture == Acall + Bcall) && (Agesture != Acall + Bcall)){
                    Bwin ++;
                }
            }
            System.out.println(Bwin+" "+Awin);
        }
    }
    private static boolean validate(int i){
         if(i > 0 && i <=100 ) return true;
         else return false;
    }
}

相关文章

  • PAT-B 1046. 划拳(15)

    传送门 https://www.patest.cn/contests/pat-b-practise/1046 题目...

  • 1046. 划拳(15)

    描述 划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数...

  • 1046.划拳

    题目描述 划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一...

  • PAT Basic 1046. 划拳(15)(C语言实现)

    我的PAT系列文章更新重心已移至Github,欢迎来看PAT题解的小伙伴请到Github Pages浏览最新内容。...

  • PAT-B 1046 划拳(C语言)

    题目 链接:PAT (Basic Level) Practice 1046 划拳 划拳是古老中国酒文化的一个有趣的...

  • 1046 划拳 (15 分)

  • PAT 1046. Shortest Distance

    1046. Shortest Distance (20) The task is really simple: g...

  • 1046 划拳 (15分)(Python)

    划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字。如...

  • PAT 1046 划拳 (15 分)

  • PTA 1046 划拳 (15 分)

    题目 划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数...

网友评论

      本文标题:PAT-B 1046. 划拳(15)

      本文链接:https://www.haomeiwen.com/subject/ukapyttx.html