网易8.8Java笔试
//第一题 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); } System.out.println(solve(arr)); } } private static long solve(long[] arr) { long count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] == 1){ count +=0; }else if(arr[i] == 2 || arr[i] == 3){ count++; }else if (arr[i] % 2 == 0 && arr[i] !=2){ count+=arr[i] / 2; }else if(arr[i] % 2 ==1 && arr[i] != 3){ count+=arr[i] / 2; } } return count; } }
//第三题 import java.util.Scanner; public class Main03 { public static void main(String[] args) { new Main03_2().start(); } private int result; private void start(){ Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while(T-- > 0){ //当前轮的结果 result = Integer.MAX_VALUE; //n个礼物 int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int[] state = new int[n]; //对于一个礼物有3种状态 要么分给A -1 要么给B -2 要么就不分 -3 dfs(0,state,arr); System.out.println(result); } } private void dfs(int index,int state[],int[] arr){ //分配完了 开始判断 if(index == state.length){ //如果A和B价值相等就直接更新丢弃值 int valOfA = 0,valOfB = 0,valOfOut = 0; for(int i = 0;i < state.length;i++){ if(state[i] == 1){ valOfA += arr[i]; }else if(state[i] == 2){ valOfB += arr[i]; }else if(state[i] == 3){ valOfOut += arr[i]; } } //统计完毕 if(valOfA == valOfB)result = Math.min(result,valOfOut); return; } //开始分配 state[index] = 1; dfs(index + 1,state,arr); state[index] = 2; dfs(index + 1,state,arr); state[index] = 3; dfs(index + 1,state,arr); } }
#网易##笔试题目#