语言:Java

知识点:全排列

根据案例,题意好懂,就是要对b进行全排列,然后和a进行比较。计算得分,然后比较是否成功。

这个知识点主要是进行全排列(回溯)。这里参考力扣题解(https://leetcode.cn/problems/permutations/solutions/218275/quan-pai-lie-by-leetcode-solution-2/

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        int[] a=new int[n];
        int[] b=new int[n];
        for(int i=0;i<n;i++){
            a[i]=in.nextInt();
        }
        List<Integer> list=new ArrayList<>();
        for(int i=0;i<n;i++){
            b[i]=in.nextInt();
            list.add(b[i]);
        }
        List<List<Integer>> res=new ArrayList<>();
        backtrack(n,list,res,0);//进行全排列
        int len=res.size();
        int winCount=0;
        int errCount=0;
        int pinCount=0;
        for(int i=0;i<len;i++){
            list=res.get(i);
            int temp1=0;
            int temp2=0;
            for(int j=0;j<n;j++){
                if(a[j]>list.get(j)){
                    temp1++;
                }else if(a[j]<list.get(j)){
                    temp2++;
                }
            }
            if(temp1>temp2){
                winCount++;
            }else if(temp2>temp1){
                errCount++;
            }else{
                pinCount++;
            }


        }
        System.out.println(winCount+" "+errCount+" "+pinCount);
        

    }
    public static void backtrack(int n, List<Integer> output, List<List<Integer>> res, int first) {
        // 所有数都填完了
        if (first == n) {
            res.add(new ArrayList<Integer>(output));
        }
        for (int i = first; i < n; i++) {
            // 动态维护数组
            Collections.swap(output, first, i);
            // 继续递归填下一个数
            backtrack(n, output, res, first + 1);
            // 撤销操作
            Collections.swap(output, first, i);
        }
    }
// 作者:力扣官方题解
// 链接:https://leetcode.cn/problems/permutations/solutions/218275/quan-pai-lie-by-leetcode-solution-2/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
}