给定学生的排名和参加考试的学生总数,任务是找到学生的百分位数。
The percentile of a student is the % of the number of students having marks less than him/her.
例子:
Input: Rank: 805, Total Number of Students Appeared: 97481
Output: 99.17
Explaination:
((97481 – 805) / 97481) * 100 = 99.17
Input: Rank: 65, Total Number of Students Appeared: 100
Output: 35
Explaination:
((100 – 65) / 100) * 100 = 35
方法
给出学生排名和学生总数出现时的百分比计算公式为:
((Total Students – Rank) / Total Students) * 100
下面是上述公式的实现:
C++
C++
// C++ program to calculate Percentile
// of a student based on rank
#include
using namespace std;
// Program to calculate the percentile
float getPercentile(int rank, int students)
{
// flat variable to store the result
float result = float(students - rank)
/ students * 100;
// calculate and return the percentile
return result;
}
// Driver Code
int main()
{
int your_rank = 805;
int total_students = 97481;
cout << getPercentile(
your_rank, total_students);
}
Java
// Java program to calculate Percentile
// of a student based on rank
import java.util.*;
class GFG{
// Program to calculate the percentile
static float getPercentile(int rank, int students)
{
// flat variable to store the result
float result = (float)(students - rank)
/ students * 100;
// calculate and return the percentile
return result;
}
// Driver Code
public static void main(String[] args)
{
int your_rank = 805;
int total_students = 97481;
System.out.print(getPercentile(
your_rank, total_students));
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program to calculate Percentile
# of a student based on rank
# Program to calculate the percentile
def getPercentile(rank, students) :
# flat variable to store the result
result = (students - rank) / students * 100;
# calculate and return the percentile
return result;
# Driver Code
if __name__ == "__main__" :
your_rank = 805;
total_students = 97481;
print(getPercentile(your_rank, total_students));
# This code is contributed by Yash_R
C#
// C# program to calculate Percentile
// of a student based on rank
using System;
class GFG{
// Program to calculate the percentile
static float getPercentile(int rank, int students)
{
// flat variable to store the result
float result = (float)(students - rank)
/ students * 100;
// calculate and return the percentile
return result;
}
// Driver Code
public static void Main(String[] args)
{
int your_rank = 805;
int total_students = 97481;
Console.Write(getPercentile(
your_rank, total_students));
}
}
// This code is contributed by Princi Singh
Javascript
输出:
99.1742
性能分析:
- 时间复杂度:在上述方法中,我们能够使用公式在恒定时间内计算百分位数,因此时间复杂度为O(1) 。
- 辅助空间复杂度:在上述方法中,除了几个恒定大小的变量之外,我们没有使用任何额外的空间,因此辅助空间复杂度为O(1) 。