华为OD 田忌赛马(java)

题目描述:

给定两个只包含数字的数组a,b,调整数组 a 里面数字的顺序,使得尽可能多的 a[i] >b[i]。数组  a和 b 中的数字各不相同。

输出所有可以达到最优结果的 a 数组的数量

输入描述:

输入的第一行是数组 a 中的数字,其中只包含数字,每两个数字之间相隔一个空格,a 数组大小不超过 10

输入的第二行是数组 b 中的数字,其中只包含数字,每两个数字之间相隔一个空格,b数组大小不超过 10

输出描述:

输出所有可以达到最优结果的 a 数组数量

示例1:

输入:

11 8 20

10 13 7

输出:

1

说明:

最优结果只有一个, a =[11,20,8],故输出 1

示例2:

输入:

11 12 20

10 13 7

输出:

2

说明:有两个 a 数组的排列可以达到最优结果 [12,20,11]和11,20,12] ,故输出 2。

代码 

public class Main {
    public static List<Integer> l2 =new ArrayList<>();
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] s1 = in.nextLine().split(" ");
        String[] s2 = in.nextLine().split(" ");

        List<Integer> list = new ArrayList<Integer>();
        for (String value : s1) {
            list.add(Integer.parseInt(value));
        }
        for (String string : s2) {
            l2.add(Integer.parseInt(string));
        }

        Collections.sort(l2);
        f(list,0);
        System.out.println(s);
    }
    public static int s=0;
    public static void f(List<Integer> list, int index) {
        if (index == list.size()) {
            s=0;
            int max=0;
            int t=bj(list,l2);
            if(t>max){
                max=t;
                s++;
            }else if(t==max){
                s++;
            }

            return;
        }
        for (int i = index; i < list.size(); i++) {
            Collections.swap(list, index, i);
            f(list, index + 1);
            Collections.swap(list, index, i);
        }
    }
    public static int bj(List<Integer> list, List<Integer> l2) {
        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i)>l2.get(i)){
                count++;
            }
        }
        return count;
    }
}

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值