题解 | 纸牌游戏-Java

纸牌游戏

https://www.nowcoder.com/practice/74f915e366094b19b94334de8b68e733

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        
        while (t-- > 0) {
            int a1 = scanner.nextInt();
            int a2 = scanner.nextInt();
            int b1 = scanner.nextInt();
            int b2 = scanner.nextInt();
            
            int count = 0;
            
            // Alex的两种翻牌顺序
            int[][] alexOrders = {{a1, a2}, {a2, a1}};
            // Bob的两种翻牌顺序
            int[][] bobOrders = {{b1, b2}, {b2, b1}};
            
            // 遍历所有可能的翻牌顺序组合
            for (int[] alex : alexOrders) {
                for (int[] bob : bobOrders) {
                    // 计算双方赢得的回合数
                    int alexWins = 0;
                    int bobWins = 0;
                    
                    if (alex[0] > bob[0]) {
                        alexWins++;
                    } else if (alex[0] < bob[0]) {
                        bobWins++;
                    }
                    
                    if (alex[1] > bob[1]) {
                        alexWins++;
                    } else if (alex[1] < bob[1]) {
                        bobWins++;
                    }
                    
                    // 当Alex赢得的回合数多于Bob时,才是Alex获胜
                    if (alexWins > bobWins) {
                        count++;
                    }
                }
            }
            
            System.out.println(count);
        }
        
        scanner.close();
    }
}


全部评论

相关推荐

活泼的代码渣渣在泡池...:哈哈哈挺好的,我也上岸美团了,不说了,我又接了一单
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务