题目描述:
给定两个只包含数字的数组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;
}
}