题解 | 小欧的选数乘积

小欧的选数乘积

https://www.nowcoder.com/practice/a94f523ebe424d0481533dc9e6138724

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long x = in.nextInt();
        long y = in.nextInt();

        int n = in.nextInt();
        int[] timesArray = new int[n];
        int i = 0;
        while (i < n) { // 注意 while 处理多个 case

            timesArray[i] = in.nextInt();
            i++;
            // int b = in.nextInt();
            // System.out.println(a + b);
        }

        if (x >= y) {
            System.out.println(0);
            return;
        }
        

        Arrays.sort(timesArray);
        int times = 0;
        int lastNum = -1;

        for (int j = timesArray.length - 1; j >= 0; j--) {
            int CurNum = timesArray[j];
            if (CurNum == lastNum) {
                continue;
            }

            x *= CurNum;
            times++;
            lastNum = CurNum;
            if (x >= y) {
                break;
            }
        }


        if (x < y) {
            times = -1;
        }
        System.out.println(times);

        // 注意 hasNext 和 hasNextLine 的区别
        // while (in.hasNextInt()) { // 注意 while 处理多个 case
        //     int a = in.nextInt();
        //     int b = in.nextInt();
        //     System.out.println(a + b);
        // }
    }
}

全部评论

相关推荐

LZStarV:冲就好了,就算真的是字节也冲,面评脏了大不了等三四个月就淡了,而且等到那个时候实力进步了选择还多,何必拘泥于字节
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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