📜  用于根据排名计算学生百分比的程序

📅  最后修改于: 2021-05-06 18:13:58             🧑  作者: Mango

给定学生排名和参加考试的学生总数,任务是找到学生的百分位数。

例子:

方法
给出学生排名和学生总数出现时的百分比计算公式为:

下面是上述公式的实现:

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)