计算学生百分比的程序
给定一个包含学生分数的数组,任务是计算学生的百分位数。百分位数根据以下规则计算:
The percentile of a student is the % of the number of students having marks less than him/her.
例子:
Input: arr[] = { 12, 60, 80, 71, 30 }
Output: { 0, 50, 100, 75, 25 }
Explanation:
Percentile of Student 1 = 0/4*100 = 0 (out of other 4 students no one has marks less than this student)
Percentile of Student 2 = 2/4*100 = 50 (out of other 4 students, 2 have marks less than this student)
Percentile of Student 3 = 4/4*100 = 100 (out of other 4 students, all 4 have marks less than this student)
Percentile of Student 4 = 3/4*100 = 75 (out of other 4 students, 3 have marks less than this student)
Percentile of Student 5 = 1/4*100 = 25 (out of other 4 students only 1 has marks less than this student)
方法:
- 所以基本上,百分位数是某个分数低于该数字的数字。
- 例如:如果在一次考试中,学生的百分位数是 75,那么这意味着该学生的得分超过了参加考试的学生的 75%。
- 现在,为了计算百分位数,我们有以下公式:
百分比 =(得分低于或等于期望学生的学生人数/学生总数 - 1)* 100
下面是上述方法的实现:
C++
// C++ program to calculate Percentile of Students
#include
using namespace std;
// Function to calculate the percentile
void percentile(int arr[], int n)
{
int i, j, count, percent;
// Start of the loop that calculates percentile
for (i = 0; i < n; i++) {
count = 0;
for (int j = 0; j < n; j++) {
// Comparing the marks of student i
// with all other students
if (arr[i] > arr[j]) {
count++;
}
}
percent = (count * 100) / (n - 1);
cout << "\nPercentile of Student "
<< i + 1 << " = " << percent;
}
}
// Driver Code
int main()
{
int StudentMarks[] = { 12, 60, 80, 71, 30 };
int n = sizeof(StudentMarks) / sizeof(StudentMarks[0]);
percentile(StudentMarks, n);
return 0;
}
Java
// Java program to calculate Percentile of Students
class GFG
{
// Function to calculate the percentile
static void percentile(int arr[], int n)
{
int i, count, percent;
// Start of the loop that calculates percentile
for (i = 0; i < n; i++)
{
count = 0;
for (int j = 0; j < n; j++)
{
// Comparing the marks of student i
// with all other students
if (arr[i] > arr[j])
{
count++;
}
}
percent = (count * 100) / (n - 1);
System.out.print("\nPercentile of Student "
+ (i + 1) + " = " + percent);
}
}
// Driver Code
public static void main(String[] args)
{
int[] StudentMarks = { 12, 60, 80, 71, 30 };
int n = StudentMarks.length;
percentile(StudentMarks, n);
}
}
// This code is contributed by Rajput-Ji
Python
# Python3 program to calculate Percentile of Students
# Function to calculate the percentile
def percentile(arr, n):
i, j = 0, 0
count, percent = 0, 0
# Start of the loop that calculates percentile
while i < n:
count = 0
j = 0
while j < n:
# Comparing the marks of student i
# with all other students
if (arr[i] > arr[j]):
count += 1
j += 1
percent = (count * 100) // (n - 1)
print("Percentile of Student ", i + 1," = ", percent)
i += 1
# Driver Code
StudentMarks = [12, 60, 80, 71, 30]
n = len(StudentMarks)
percentile(StudentMarks, n)
# This code is contributed by mohit kumar 29
C#
// C# program to calculate Percentile of Students
using System;
class GFG
{
// Function to calculate the percentile
static void percentile(int []arr, int n)
{
int i, count, percent;
// Start of the loop that calculates percentile
for (i = 0; i < n; i++)
{
count = 0;
for (int j = 0; j < n; j++)
{
// Comparing the marks of student i
// with all other students
if (arr[i] > arr[j])
{
count++;
}
}
percent = (count * 100) / (n - 1);
Console.Write("\nPercentile of Student "
+ (i + 1) + " = " + percent);
}
}
// Driver Code
public static void Main()
{
int[] StudentMarks = {12, 60, 80, 71, 30};
int n = StudentMarks.Length;
percentile(StudentMarks, n);
}
}
// This code is contributed by AnkitRai01
Javascript
Percentile of Student 1 = 0
Percentile of Student 2 = 50
Percentile of Student 3 = 100
Percentile of Student 4 = 75
Percentile of Student 5 = 25