牛客模拟笔试5月场

原题参考:https://blog.youkuaiyun.com/sinat_34136785/article/details/80703895

题目一:扑克牌

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a1 = sc.nextInt();
        int a2 = sc.nextInt();
        int a3 = sc.nextInt();
        int a = a1 + a2 + a3;
        int b1 = sc.nextInt();
        int b2 = sc.nextInt();
        int b3 = sc.nextInt();
        int b = b1 + b2 + b3;
        
        int[] left = new int[14];//记录每个点数的牌剩余的张数,0位空着
        for(int i = 1; i < left.length; i++) {
            left[i] = 4;
        }
        left[a1]--;
        left[a2]--;
        left[a3]--;
        left[b1]--;
        left[b2]--;
        left[b3]--;
        
        int all = pailie(46,2); //所有可能情况
        int count = 0;          //记录牛牛获胜情况的次数
        for(int i = 1; i <= 13; i++) {
            for(int j = 1; j <= 13; j++) {
                
                if(i == j && left[i] >= 2) { //两张牌相等且有效
                    if(a > b)
                        count += pailie(left[i], 2);
                }
                else if(i != j && left[i] >0 && left[j] > 0) { //两张牌不等且有效
                    if(a + i > b + j)
                        count += left[i] * left[j];
                }
            }
        }
        double p = count * 1.0 / all; //牛牛获胜的概率
        String result = String.format("%.4f", p);
        System.out.println(result);
    }
    private static int pailie(int n, int m) { //返回“从n个数中选出m个数的排列”的个数
        int ans = 1;
        for(int i = n; i > n-m; i--)
            ans = ans * i;
        return ans;
    }
}

题目二:数星星

出错:超时了。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(); //星星个数
        int[] x = new int[n];
        int[] y = new int[n];
        for(int i = 0; i < n; i++) { //记录每个星星的坐标x[i],y[i]
            x[i] = sc.nextInt();
            y[i] = sc.nextInt();
        }
        
        int m = sc.nextInt(); //矩形个数
        for(int j = 0; j < m; j++) { //每一个问题
            int a1 = sc.nextInt(); //左上角x
            int b1 = sc.nextInt(); //左上角y
            int a2 = sc.nextInt(); //右下角x
            int b2 = sc.nextInt(); //右下角y
            int ans = 0;
            for(int i = 0; i < n; i++) { //扫描每颗星星
                if(a1 <= x[i] && x[i] <= a2) {
                    if(b1 <= y[i] && y[i] <= b2)
                        ans++;
                }
            }
            System.out.println(ans);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值