10.22 58同城笔试题
第一题
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main2 { public static void main(String[] args) throws IOException { BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); String[] str = buf.readLine().split(","); int a = 1, n; int count = 0; int sum = 0; boolean isEnd = false; for (n = 1; n <= 16; n++) { if (n > 1) { a = a * 3; } if (isEnd) { break; } for (int i = count; i < ((a + count) < str.length ? a + count : str.length); i++) { if (n %2 != 0) { if (!str[i].equals("None")) { sum += Integer.parseInt(str[i]); } } } count += a; if (count >= str.length) { isEnd = true; } } System.out.println(sum); } }
第二题
import java.util.Scanner; public class Main1 { public static void main(String[] args) { Scanner s1 = new Scanner(System.in); int n = s1.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = s1.nextInt(); } System.out.println(isTrue(nums)); } public static int isTrue(int[] nums) { int sum = 0; for (int num : nums) { sum += num; } if (sum % 2 != 0) { return 0; } int amount = sum / 2; int[] dp = new int[amount + 1]; for (int i = 0; i < nums.length; i++) { for (int j = amount; j >= nums[i]; j--) { dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i]); } } if (dp[amount] == sum / 2) { return amount; } else { return 0; } } }
第三题
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s1 = new Scanner(System.in); int n = s1.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s1.nextInt(); } if (n == 1) { System.out.println(arr[0]);; } else { int[] dp = new int[n]; dp[0] = arr[0]; dp[1] = Math.max(arr[0], arr[1]); for (int i = 2; i < n; i++) { dp[i] = Math.max(dp[i - 1], dp[i - 2] + arr[i]); } System.out.println(dp[n - 1]); } } }#校招##笔试##58集团##58同城##java#