拼多多 第一题,第三题,求帮忙看看哪错啦?
第一题:题目中的测试用例过了,我在想是不是我的输入输出有问题?
第三题:排列组合那没问题,题目中的测试用例也过了,我在想是不是我的输入输出有问题?public static void main(String[] args) { Scanner sc=new Scanner(System.in); String nn=sc.nextLine(); if(nn==null||nn.length()==0){ System.out.println(0); } int n=new Integer(nn); if(n==0||n<4||n%2!=0) System.out.println(0); String str=sc.nextLine(); String[] strings=str.split(" "); int[] arr=new int[strings.length]; for(int i=0;i<strings.length;i++){ arr[i]=new Integer(strings[i]); } Arrays.sort(arr); int max=arr[0]+arr[n-1]; int min=arr[n/2-1]+arr[n/2]; System.out.println(max-min); }
public static void main(String[] args) { Scanner sc = new Scanner(System.in); //String str = "{31,18,19,1,25}"; int d = 10; double res = 0; String str = sc.next(); if(str!=null&&str.length()>2){ str=str.substring(1,str.length()-1); String[] strings=str.split(","); int[] arr=new int[strings.length]; for(int i=0;i<strings.length;i++){ arr[i]=Integer.valueOf(strings[i]); } d = sc.nextInt(); PDD3 obj = new PDD3(); obj.combinationSum(arr, 2, d); res = (double) count / total; System.out.println(String.format("%.6f", res)); }else{ System.out.println(String.format("%.6f", res)); } } static int count = 0; static int total = 0; public List<List<Integer>> combinationSum(int[] candidates, int target, int d) { List<List<Integer>> result = new ArrayList<>(); Arrays.sort(candidates); backTrack(candidates, result, new ArrayList<Integer>(), target, 0, d); return result; } private void backTrack(int[] candidates, List<List<Integer>> result, ArrayList<Integer> list, int remain, int start, int d) { if (remain == 0) { if (Math.abs(list.get(0) - list.get(1)) <= d) { count++; } total++; result.add(new ArrayList<>(list)); } else { for (int i = start; i < candidates.length; i++) { list.add(candidates[i]); backTrack(candidates, result, list, remain - 1, i + 1, d); list.remove(list.size() - 1); } } }