微众4.20笔试(java) 答案
纪念第一次ak
第一题:游戏 比伤害
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] m1 = new long[n]; long[] m2 = new long[n]; for(int i = 0;i<n;i++) { m1[i] = sc.nextLong(); } for(int i = 0;i<n;i++) { m2[i] = sc.nextLong(); } long sum1 = 0; long sum2 = 0; long res = 0; for(int i = 0;i<n;i++) { sum1 += m1[i]+res; sum2 += m2[i]; if(sum1 <= sum2) { long temp = 0; if((sum2-sum1+1)%(i+1) == 0) { temp = (sum2-sum1+1)/(i+1); } else temp = (sum2-sum1+1)/(i+1)+1; res += temp; sum1 += temp*(i+1); } } System.out.println(res); }第二题 找递增子序列
public class Test4 { static Long cnt; public static void main(String[] args) { cnt = 0L; Scanner sc =new Scanner(System.in); int n = sc.nextInt(); int[] nums = new int[n]; for(int i = 0;i<n;i++) { nums[i] = sc.nextInt(); } List<Integer> combine = new ArrayList<>(); dfs(combine,nums,0); System.out.println(cnt); } public static void dfs(List<Integer> combine,int[] nums,int begin) { if(combine.size() == 1)cnt = (cnt+1)%1000000007; if(combine.size()>1) { int temp = combine.size(); if(combine.get(temp-1) > combine.get(temp-2)) cnt = (cnt+1)%1000000007; else return; } for(int i = begin;i<nums.length;i++) { combine.add(nums[i]); dfs(combine,nums,i+1); combine.remove(combine.size()-1); } } }第三题 找k字符串
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); char c = s.charAt(0); String s1 = sc.next(); int len = s1.length(); int res = 0; for (int i = 0; i < len; i++) { int temp = k; for (int j = i; j < len; j++) { if (temp == 0 && s1.charAt(j) == c) break; if (s1.charAt(j) == c) { temp--; } if (temp == 0) res++; } } System.out.println(res); }这题res变量开始用long修饰过91%,后来改int修饰变100%了,不道为啥