题解 | 田忌赛马

田忌赛马

https://www.nowcoder.com/practice/49d799f65a0749588e9cd7e6135a4a9a

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        List<Integer> a = new ArrayList<>(3);
        List<Integer> b = new ArrayList<>(3);
            a.add(in.nextInt()) ;
            a.add(in.nextInt()) ;
            a.add(in.nextInt()) ;
            b.add(in.nextInt()) ;
            b.add(in.nextInt()) ;
            b.add(in.nextInt()) ;
        System.out.println(win(a,b)?"Yes":"No");
    }
    /**
     * 田忌赛马
     * @param a
     * @param b
     * @return
     */

        private static boolean win(List<Integer> a, List<Integer>  b){
        // 1 2 3
        // 1 2 3
        Collections.sort(a);
        Collections.sort(b);
        int winRounds = a.size() / 2+1;
        int i = 0;
        while (i<winRounds){
            if (a.get(i) >= b.get(b.size()-winRounds+i)){
                return false;
            }
             i++;
        }
        return true;

    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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