考核内容:
在指定的题库中自主选择不少于 15 道算法题并完成解题,其中题目难度分配如下:
- 简单题不少于 10 道
- 中等题不少于 4 道
- 困难题不少于 1 道
解答代码
20. 百分位数(中等)
代码实现:
import java.util.Arrays;
public class Main {
public static int solution(String data) {
// 首先将输入的字符串转换为整数数组
String[] strNums = data.split(",");
int[] nums = new int[strNums.length];
for (int i = 0; i < strNums.length; i++) {
nums[i] = Integer.parseInt(strNums[i]);
}
// 对数组进行排序
Arrays.sort(nums);
// 计算 80 百分位数的位置
int position = (int) Math.ceil(nums.length * 0.8);
// 返回该位置的元素
return nums[position - 1];
}
public static void main(String[] args) {
// You can add more test cases here
System.out.println(solution("10,1,9,2,8,3,7,4,6,5") == 8);
System.out.println(solution("1,0,8,7,3,9,12,6,4,15,17,2,14,5,10,11,19,13,16,18") == 15);
System.out.println(solution(
"76,100,5,99,16,45,18,3,81,65,102,98,36,4,2,7,22,66,112,97,68,82,37,90,61,73,107,104,79,14,52,83,27,35,93,21,118,120,33,6,19,85,49,44,69,53,67,110,47,91,17,55,80,78,119,15,11,70,103,32,9,40,114,26,25,87,74,1,30,54,38,50,8,34,28,20,24,105,106,31,92,59,116,42,111,57,95,115,96,108,10,89,23,62,29,109,56,58,63,41,77,84,64,75,72,117,101,60,48,94,46,39,43,88,12,113,13,51,86,71") == 96);
}
}
运行结果: