10/11满帮笔试 AK
第一题 弯腰最少次数 ac
import java.util.Scanner; public class Q1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); while (n > 0){ n--; int m = scanner.nextInt(); int[] a = new int[m]; int[] b = new int[m]; int maxa = 0; int maxb = 0; for (int i = 0; i < m; i++) { a[i] = scanner.nextInt(); maxa = Math.max(maxa , a[i]); } for (int i = 0; i < m; i++) { b[i] = scanner.nextInt(); maxb = Math.max(maxb , b[i]); } int res = 0; for (int i = 0; i < m; i++) { res += Math.max(maxa - a[i] , maxb - b[i]); } System.out.println(res); } } }
第二题 老鼠逃跑最短路径和 ac
贪心算法即可,按照距离差排序,好久没写代码,差点忘了排序的写法,和c++正好反着package bishi.shunfeng.mb; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; class t{ long a; long b; double jl; t(long a , long b){ this.a = a; this.b = b; } } public class Q2 { static long ax,ay,bx,by; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); ax = scanner.nextLong(); ay = scanner.nextLong(); bx = scanner.nextLong(); by = scanner.nextLong(); t[] nums = new t[n]; for (int i = 0; i < n; i++) { long a = scanner.nextLong(); long b = scanner.nextLong(); nums[i] = new t(a ,b); nums[i].jl =find(nums[i].a , nums[i].b); } Arrays.sort(nums, new Comparator<t>() { @Override public int compare(t o1, t o2) { if(o1.jl <= o2.jl) return -1; return 1; } }); for (int i = 0; i < n; i++) { System.out.println(nums[i].jl); } double res = 0.0; for (int i = k; i < n; i++) { res += Math.sqrt((nums[i].a - bx) * 1.0 * (nums[i].a - bx)*1.0 + (nums[i].b - by)*1.0 * (nums[i].b - by)*1.0); } for (int i = 0; i < k ; i++) { res += Math.sqrt((nums[i].a - ax) * 1.0 * (nums[i].a - ax)* 1.0 + (nums[i].b - ay) * 1.0 * (nums[i].b - ay)* 1.0); } System.out.println(res); } private static double find(long a , long b){ return Math.sqrt((a - ax) * (a - ax)*1.0 + (b - ay) * (b - ay) *1.0) - Math.sqrt((a - bx) * (a - bx)*1.0 + (b - by) * (b - by)*1.0); } }