赞
踩
给定两个只包含数字的数组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
7 8 9 8
1 2 10 3
输出:12
解题思路:
考察全排列,排列之前队数组a进行排序,目的是去重
- public static int maxMatch = 0; //数组1大于数组2最大个数
- public static int globalCount =0; //最大计数
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- while(sc.hasNext()) {
- maxMatch = 0;
- globalCount =0;
- String[] s1 = sc.nextLine().split(" ");
- String[] s2 = sc.nextLine().split(" ");
- int nums1[] = new int[s1.length];
- int nums2[] = new int[s2.length];
- for(int i=0; i< s1.length;i++) {
- nums1[i] = Integer.valueOf(s1[i]);
- }
- for(int i=0; i< s2.length;i++) {
- nums2[i] = Integer.valueOf(s2[i]);
- }
- int[] vis = new int[nums1.length];
- Arrays.sort(nums1); //排序,方便去重
- solution(nums1, nums2, 0, vis, 0);
-
- System.out.println(globalCount);
- }
- }
- public static int solution(int nums1[], int nums2[], int nums2Index,
- int[] vis, int greateMatchNum){
- if(nums2Index == nums2.length) {// 说明已经匹配完毕,开始算账
- if(greateMatchNum > maxMatch) {
- maxMatch = greateMatchNum;
- globalCount = 1; // 重置1
- }else if(greateMatchNum == maxMatch) {
- globalCount +=1;
- }
- return 0;
- }
- for(int i=0; i< nums1.length; i++) {
- //这个位置的数没有被标记使用,可以用
- if(vis[i] != -1) {
- //前一个数在这个位置已经排列过,重复,跳过
- if(i>0 && nums1[i] == nums1[i-1] && vis[i-1] != -1) {
- continue;
- }
- vis[i] = -1; // 标记已用
- solution(nums1, nums2, nums2Index+1, vis,(nums1[i] > nums2[nums2Index]?greateMatchNum+1:greateMatchNum));
- vis[i] = 0; // 恢复标记
- }
- }
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。