10/11满帮笔试
第一题 弯腰最少次数 ac
package 满帮; import java.util.*; /** * @time 2022-10-11-19:05 */ public class Q1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); // while (in.hasNextInt()) { int T = in.nextInt(); for (int i = 0; i < T; i++) { int n = in.nextInt();//people numbers int[] apples = new int[n]; int[] candles = new int[n]; int maxApple = 0; for (int j = 0; j < n; j++) { apples[j] = in.nextInt(); maxApple = Math.max(maxApple, apples[j]); } int maxCandle = 0; for (int j = 0; j < n; j++) { candles[j] = in.nextInt(); maxCandle = Math.max(maxCandle, candles[j]); } //补足使得每个盒子苹果和糖果对应相等 // if (n == 1) { // System.out.println(0); // continue; // } int count = 0; for (int j = 0; j < n; j++) { while (apples[j] < maxApple && candles[j] < maxCandle) { int k = Math.min(maxApple - apples[j], maxCandle - candles[j]); candles[j] += k; apples[j] += k; count+=k; } if (apples[j] == maxApple && candles[j] < maxCandle) { count += maxCandle - candles[j]; } if (candles[j] == maxCandle && apples[j] < maxApple) { count += maxApple - apples[j]; } } System.out.println(count); } } // } }
第二题 老鼠逃跑最短路径和 0
n只老鼠 A洞穴能躲k只老鼠 B洞穴n-k只
给定A , B 坐标
然后给出n个老鼠坐标
求鼠鼠们最短的路径总和
不会啊,没时间做了,就瞎做一通,有无大佬解答一番
package 满帮; import java.util.Scanner; /** * @time 2022-10-11-19:41 */ public class Q2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int n = in.nextInt(); int k = in.nextInt(); // int xa = in.nextInt(); int ya = in.nextInt(); int xb = in.nextInt(); int yb = in.nextInt(); double[][] dis = new double[n][2]; double avg_a = 0; double avg_b = 0; for (int i = 0; i < n; i++) { int x = in.nextInt(); int y = in.nextInt(); dis[i][0] = Math.sqrt((xa - x) * 2 + (ya - y) * 2); dis[i][1] = Math.sqrt((xb - x) * 2 + (yb - y) * 2); avg_a += dis[i][0] / n; avg_b += dis[i][1] / n; } double res = 0; int index = 0; for (int i = 0; i < n; i++) { if (Math.abs(dis[i][0] - avg_a) < Math.abs(dis[i][1] - avg_b) && index < k) { res += dis[i][0]; index++; } res+=dis[i][1]; } System.out.println(res); } } }#笔试成绩不好也能得到面试机会吗#