输入第一行为数字个数n (n ≤ 20) 第二行为n个数xi (1 ≤ xi ≤ 100000)
输出最小不能由n个数选取求和组成的数
3 5 1 2
4
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bf.readLine()); String[] s = bf.readLine().split(" "); int[] x = new int[n]; for(int i = 0; i < n; i++) x[i] = Integer.parseInt(s[i]); Arrays.sort(x); if(x[0]-1!=0){ System.out.println(1); }else{ int[] dp = new int[n]; dp[0] = 1; int i = 1; for(; i < n; i++){ if(dp[i-1]<x[i]-1) break; dp[i] = dp[i-1]+x[i]; } System.out.println(dp[i-1]+1); } } }
import java.util.*; //先排序,记录可以累加到的和在map中,按顺序遍历,一旦出现空缺元素那就是不能累加到的第一个元素 public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String line = scanner.nextLine(); int n = Integer.parseInt(line); line = scanner.nextLine(); String []s = line.split(" "); int []arr = new int[n]; for (int i = 0;i < n;i ++) { arr[i] = Integer.parseInt(s[i]); } System.out.println(min(arr)); } public static int min(int []arr) { Arrays.sort(arr); if (arr[0] != 1) { return 1; } int max = arr[0]; LinkedHashMap<Integer, Integer> map = new LinkedHashMap<>(); Set<Integer> set = map.keySet(); for (int i = 0;i < arr.length;i ++) { if (arr[i] - max > 1)return max + 1; List<Integer> list = new ArrayList<>(); for (int j : set) { list.add(j + arr[i]); max = Math.max(max, j + arr[i]); } for (int num : list) { map.put(num, 1); } map.put(arr[i], 1); } return max + 1; } }
//举个例子其实就明白了 /** 比如:1 2 3 4 12 --> 1+2+3+4=10 --> 10+1<12 --> 结果为10+1=11 */ import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int count = in.nextInt(); int[] array = new int[count]; for (int i = 0; i < count; i++) array[i] = in.nextInt(); Arrays.sort(array); int sum = 0; for (int i = 0; i < count; i++) { if (sum + 1 < array[i]) break; sum += array[i]; } System.out.println(sum + 1); } } }
import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { //输入 Scanner sc = new Scanner(System.in); int count = sc.nextInt(); int[] nums = new int[count]; for (int i = 0; i < count; i++) { nums[i] = sc.nextInt(); } //排序 Arrays.sort(nums); //特例,没有1直接输出1 if (nums[0] > 1) { System.out.println(1); return; } //依次求和 int sum = 0; for (int i = 0; i < count - 1; i++) { sum += nums[i]; if (sum + 1 < nums[i + 1]){ System.out.println(sum + 1); return; } } System.out.println(sum + nums[count - 1] + 1); } }
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = sc.nextInt(); } System.out.println(Main(arr,n)); sc.close(); } public static void CombineIncrease(int[] arr, int start, int[] result, int count, int num, Set<Integer> set){ int i = 0; for(i = start; i < arr.length + 1 - count; i++){ result[count - 1] = i; if(count - 1 == 0){ int j = 0; int sum = 0; for(j = num - 1; j >= 0; j--){ sum += arr[result[j]]; } set.add(sum); }else{ CombineIncrease(arr, i + 1, result, count - 1, num, set); } } } private static int Main(int[] arr, int n) { Set<Integer> set = new HashSet<Integer>(); for(int i = 1; i <= n; i++){ int[] result = new int[i]; CombineIncrease(arr, 0, result, i, i, set); } for(int i = 1; i <= set.size(); i++){ if(!set.contains(i)){ return i; } } return set.size()+1; } }