四道题,分数分别为10,20,30,40。
本人结果为100%,100%,92%,100%。
第一题100%
package netease; import java.util.*; public class Main1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int target = sc.nextInt()+sc.nextInt(); int bias = sc.nextInt(); int[] teams = new int[sc.nextInt()]; for(int i=0;i<teams.length;i++){ teams[i] = sc.nextInt()+sc.nextInt(); } int[] person = new int[sc.nextInt()]; for(int i=0;i<person.length;i++){ person[i] = sc.nextInt(); } System.out.println(getPerson(person, target, bias)+getTeams(teams, target, bias)); } public static int getPerson(int[] person, int target, int bias){ int ret = 0; for(int i=0;i<person.length;i++){ for(int j=i+1;j<person.length;j++){ if(Math.abs(person[i]+person[j]-target)<=bias) ret++; } } return ret; } public static int getTeams(int[] teams, int target, int bias){ int ret = 0; for(int i:teams){ if(Math.abs(i-target)<=bias) ret++; } return ret; } }第二题100%
package netease; import java.util.*; public class Main2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] time = new int[4]; for(int i=0;i<4;i++){ time[i] = sc.nextInt(); } int[] road = new int[n]; for(int i=0;i<n;i++){ road[i] = sc.nextInt(); } System.out.println(solve(road, time)); } /** * * @param road * @param time 0-平移 1-爬坡 2-下坡 3-切换 * @return */ public static int solve(int[] road, int[] time){ int l = road.length; if(l==0) return 0; int[][] dp = new int[2][l]; dp[0][l-1] = 0; dp[1][l-1] = 0; for(int i=l-2;i>=0;i--){ int gap = road[i]-road[i+1]; if(gap>=2){ dp[0][i] = dp[0][i+1] + time[2]; dp[1][i] = dp[0][i+1] + time[2] + time[3]; }else if(gap<=-2){ dp[0][i] = dp[1][i + 1] + time[2] + time[3]; dp[1][i] = dp[1][i + 1] + time[2]; }else{ int t1 = 0, t2 = 0; if(gap==0){ t1 = time[0]; t2 = time[0]; }else if(gap==1){ t1 = time[2]; t2 = time[1]; }else{ t1 = time[1]; t2 = time[2]; } dp[0][i] = Math.min(dp[1][i+1] + time[3] + t2, dp[0][i+1] + t1); dp[1][i] = Math.min(dp[0][i+1] + time[3] + t1, dp[1][i+1] + t2); } } return dp[0][0]; } }第三题92%,还差个判断是否循环引用,懒得搞了
package netease; import java.util.*; public class Main3 { //null 8% error 8% public static void main(String[] args) { Scanner sc = new Scanner(System.in); int count = sc.nextInt(); sc.nextLine(); String[] input = new String[count]; for(int i=0;i<count;i++){ input[i] = sc.nextLine(); } String query = sc.nextLine(); System.out.println(solve(input, query)); } static HashMap<String, String> map; static HashMap<String, List<String>> waitq; public static String solve(String[] input, String query){ if(input.length==0) return "NULL"; map = new HashMap<>(); waitq = new HashMap<>(); for(String s:input){ int idx = 0; while(idx<s.length()&&s.charAt(idx)==' ') idx++; //为空行或者注释行 if(idx==s.length()||s.charAt(idx)=='#') continue; int start = idx; while(idx<s.length()&&s.charAt(idx)!=' '&&s.charAt(idx)!='=') idx++; if(idx>=s.length()||idx==start) return "ERROR"; String key = s.substring(start, idx); idx = s.indexOf('=', idx); if(idx<0) return "ERROR"; idx++; //去= value中间的空格 while(idx<s.length()&&s.charAt(idx)==' ') idx++; if(idx>=s.length()) return "ERROR"; int end = s.length()-1; while(s.charAt(end)==' ') end--; String value = s.substring(idx, end+1); if(value.indexOf('{')<0){ if(value.indexOf('}')<0){ map.put(key, value); }else{ return "ERROR"; } } StringBuilder sb = new StringBuilder(value); String rst = get(sb, 0); if(rst.equals("ERROR")) return "ERROR"; map.put(key, sb.toString()); } int left = 0, right = query.length()-1; while(left<query.length()&&query.charAt(left)==' ') left++; while(right>=left&&query.charAt(right)==' ') right--; String q = query.substring(left, right+1); if(!map.containsKey(q)) return "NULL"; return map.get(q); } public static String get(StringBuilder sb, int idx){ while(idx<sb.length()&&sb.charAt(idx)!='{'&&sb.charAt(idx)!='}') idx++; if(idx>=sb.length()) return ""; if(sb.charAt(idx)=='}') return "ERROR"; int right = sb.indexOf("}", idx); if(right<0||idx+1==right) return "ERROR"; String want = sb.substring(idx+1, right); if(map.containsKey(want)){ sb.delete(idx, right+1); sb.insert(idx, map.get(want)); }else{ return "ERROR"; } return get(sb, idx); } }第四题,其实想明白了就是简单的数学题
package netease; import java.util.*; public class Main4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int len = sc.nextInt()*100; int count = sc.nextInt(); int range = sc.nextInt()*100; int[] loc = new int[count]; int[] v = new int[count]; for(int i=0;i<count;i++){ loc[i] = sc.nextInt()*100; } for(int i=0;i<count;i++){ v[i] = sc.nextInt(); } int ret = solve(loc, v, range, len); StringBuilder sb = new StringBuilder(); sb.append(ret/100); ret %= 100; sb.append('.'); if(ret<10){ sb.append('0'); sb.append(ret); }else{ sb.append(ret); } System.out.println(sb.toString()); } public static int solve(int[] loc, int[] v, int range, int len){ if(range*2>=len) return 0; int gap = loc[0]%len-range; if(gap<0){ gap = -gap; for(int i=0;i<loc.length;i++){ loc[i] = (loc[i]+gap)%len; } } int l1 = (loc[0]-range+len)%len; int r1 = (loc[0]+range)%len; int ret = 0; for(int i=1;i<loc.length;i++){ int at = loc[i]%len; if(at>=l1&&at<=r1) continue; if(at<l1){ if(v[i]>v[0]){ ret = Math.max(ret, (l1-at)/(v[i]-v[0])); }else{ ret = Math.max(ret, (at+len-r1)/(v[0]-v[i])); } }else{ if(v[i]>v[0]){ ret = Math.max(ret, (l1+len-at)/(v[i]-v[0])); }else{ ret = Math.max(ret, (at-r1)/(v[0]-v[i])); } } } return ret; } }
全部评论
(16) 回帖