来源牛客网 https://www.nowcoder.com/questionTerminal/f51e08c7642345fb96dd7b29792b0790
题目
小易是班级的英语课代表, 他开发了一款软件开处理他的工作。 小易的软件有一个神奇的功能,能够通过一个百分数来反应你的成绩在班上的位置。“成绩超过班级 ...% 的同学”。 设这个百分数为 p,考了 s 分,则可以通过以下式子计算得出 p: p = ( 分数不超过 s 的人数 - 1) \div÷ 班级总人数 \times 100%×100% 突然一天的英语考试之后,软件突然罢工了,这可忙坏了小易。成绩输入这些对于字写得又快又好的小易当然没有问题,但是计算这些百分数……这庞大的数据量吓坏了他。 于是他来找到你,希望他编一个程序模拟这个软件:给出班级人数 n,以及每个人的成绩,请求出某几位同学的百分数
参考的网上的二分搜索
long[] sortedArr = ... int index = Arrays.binarySearch (sortedArr, value); int first = index; int last = index; if (index >= 0) { while (first > 0 && sortedArr[first-1] == value) first--; while (last < sortedArr.length - 1 && sortedArr[last+1] == value) last++; }
以及java中的格式控制
String resStr = String.format("%.6f ",1.123456789); System.out.println(Double.parseDouble(resStr));
import java.text.DecimalFormat; double a = 1.34566777; DecimalFormat df1 = new DecimalFormat("0.000000");//不够6位补零输出 DecimalFormat df2 = new DecimalFormat("0.######");//不够6位以实际位数输出 你要哪个? System.out.println(df2.format(a)); //输出 1.345668
这个是通过了测试用例的
import java.util.Arrays;
import java.util.Scanner;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);
String studentNumStr = Scan.nextLine();
int studentNumInt = Integer.parseInt(studentNumStr);
String studentString = Scan.nextLine();
String[] studentScoresStr = studentString.split(" ");
int[] studentScoresInt = new int[studentScoresStr.length];
for(int i = 0;i<studentScoresStr.length;i++){
studentScoresInt[i] = Integer.parseInt(studentScoresStr[i]);
}
int[] studentScoresIntClone = studentScoresInt.clone();
Arrays.sort(studentScoresIntClone);
int queryNum =Integer.parseInt(Scan.nextLine());
int[] queryContent = new int[queryNum];
for (int i = 0; i < queryNum; i++){
int studentSequence = Scan.nextInt();
queryContent[i] = studentSequence;
}
for (int element:queryContent){
double res = ansQuery(element,studentScoresInt,studentScoresIntClone);
DecimalFormat format = new DecimalFormat("0.000000");
System.out.println(format.format(res));
}
}
public static double ansQuery(int studentSequence, int[] studentScoresInt, int[] sortedStudentScoresIntClone){
int target = studentScoresInt[studentSequence-1];
int index = Arrays.binarySearch(sortedStudentScoresIntClone,target);
int firstAns = index;
int lastAns = index;
if (index >= 0) {
while (firstAns > 0 && sortedStudentScoresIntClone[firstAns-1] == target)
firstAns--;
while (lastAns < sortedStudentScoresIntClone.length - 1 && sortedStudentScoresIntClone[lastAns+1] == target)
lastAns++;
}
double res = (double)lastAns/studentScoresInt.length*100;
return res;
}
}
这个是没有注意输入输出的要求:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);
System.out.println("请输入学生人数:");
String studentNumStr = Scan.nextLine();
int studentNumInt = Integer.parseInt(studentNumStr);
System.out.println("请输入成绩,以空格分隔:");
String studentString = Scan.nextLine();
String[] studentScoresStr = studentString.split(" ");
int[] studentScoresInt = new int[studentScoresStr.length];
for(int i = 0;i<studentScoresStr.length;i++){
studentScoresInt[i] = Integer.parseInt(studentScoresStr[i]);
}
int[] studentScoresIntClone = studentScoresInt.clone();
Arrays.sort(studentScoresIntClone);
System.out.println("这是输入的成绩:"+ Arrays.toString(studentScoresInt));
int queryNum =Integer.parseInt(Scan.nextLine());
for (int i = 0; i < queryNum; i++){
int studentSequence = Scan.nextInt();
double res = ansQuery(studentSequence,studentScoresInt,studentScoresIntClone);
System.out.printf("成绩超过班级 %.6f %%的同学\n",res);
}
}
public static double ansQuery(int studentSequence, int[] studentScoresInt, int[] studentScoresIntClone){
int target = studentScoresInt[studentSequence-1];
int index = Arrays.binarySearch(sortedStudentScoresIntClone,target);
int firstAns = index;
int lastAns = index;
if (index >= 0) {
while (firstAns > 0 && sortedStudentScoresIntClone[firstAns-1] == target)
firstAns--;
while (lastAns < sortedStudentScoresIntClone.length - 1 && sortedStudentScoresIntClone[lastAns+1] == target)
lastAns++;
}
double res = (double)lastAns/studentScoresInt.length*100;
return res;
}
}