给定三个数组名称[] , marks []和updates [] ,其中:
- names []包含学生的姓名。
- mark []包含同一学生的分数。
- updates []包含将用来更新这些学生的分数的整数。
任务是找到更新后分数最高的学生姓名,以及该学生的排名(即先前的排名-当前的排名)的跳跃。
注意:学生的详细信息按分数的降序排列,如果有两个以上的学生获得相等的分数(也是最高分),则选择以前排名较低的那个。
例子:
Input: names[] = {“sam”, “ram”, “geek”, “sonu”},
marks[] = {99, 75, 70, 60},
updates[] = {-10, 5, 9, 39}
Output: Name: sonu, Jump: 3
Updated marks are {89, 80, 79, 99}, its clear that sonu has the highest marks with jump of 3
Input: names[] = {“sam”, “ram”, “geek”},
marks[] = {80, 79, 75},
updates[] = {0, 5, -9}
Output: Name: ram, Jump: 1
方法:创建一个结构struct Student来存储每个学生的信息,每个学生将具有3个学生的属性名称,学生的标记,学生的prev_rank 。
现在,根据updates []的内容更新分数,然后对数组进行一次遍历,找到更新后分数最高的学生。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Structure to store the information of
// students
struct Student {
string name;
int marks = 0;
int prev_rank = 0;
};
// Function to print the name of student who
// stood first after updation in rank
void nameRank(string names[], int marks[],
int updates[], int n)
{
// Array of students
Student x[n];
for (int i = 0; i < n; i++) {
// Store the name of the student
x[i].name = names[i];
// Update the marks of the student
x[i].marks = marks[i] + updates[i];
// Store the current rank of the student
x[i].prev_rank = i + 1;
}
Student highest = x[0];
for (int j = 1; j < n; j++) {
if (x[j].marks >= highest.marks)
highest = x[j];
}
// Print the name and jump in rank
cout << "Name: " << highest.name
<< ", Jump: "
<< abs(highest.prev_rank - 1)
<< endl;
}
// Driver code
int main()
{
// Names of the students
string names[] = { "sam", "ram", "geek" };
// Marks of the students
int marks[] = { 80, 79, 75 };
// Updates that are to be done
int updates[] = { 0, 5, -9 };
// Number of students
int n = sizeof(marks) / sizeof(marks[0]);
nameRank(names, marks, updates, n);
}
Java
// Java implementation of the approach
import java.util.*;
class GFG{
static class Student
{
String name;
int marks, prev_rank;
Student()
{
this.marks = 0;
this.prev_rank = 0;
}
}
// Function to print the name of student who
// stood first after updation in rank
static void nameRank(String []names, int []marks,
int []updates, int n)
{
// Array of students
Student []x = new Student[n];
for(int i = 0; i < n; i++)
{
x[i] = new Student();
// Store the name of the student
x[i].name = names[i];
// Update the marks of the student
x[i].marks = marks[i] + updates[i];
// Store the current rank of the student
x[i].prev_rank = i + 1;
}
Student highest = x[0];
for(int j = 1; j < n; j++)
{
if (x[j].marks >= highest.marks)
highest = x[j];
}
// Print the name and jump in rank
System.out.print("Name: " + highest.name +
", Jump: " +
Math.abs(highest.prev_rank - 1));
}
// Driver code
public static void main(String[] args)
{
// Names of the students
String []names = { "sam", "ram", "geek" };
// Marks of the students
int []marks = { 80, 79, 75 };
// Updates that are to be done
int []updates = { 0, 5, -9 };
// Number of students
int n = marks.length;
nameRank(names, marks, updates, n);
}
}
// This code is contributed by pratham76
Python3
# Python3 implementation of the approach
# Function to prthe name of student who
# stood first after updation in rank
def nameRank(names, marks, updates, n):
# Array of students
x = [[0 for j in range(3)] for i in range(n)]
for i in range(n):
# Store the name of the student
x[i][0] = names[i]
# Update the marks of the student
x[i][1]= marks[i] + updates[i]
# Store the current rank of the student
x[i][2] = i + 1
highest = x[0]
for j in range(1, n):
if (x[j][1] >= highest[1]):
highest = x[j]
# Print the name and jump in rank
print("Name: ", highest[0], ", Jump: ",
abs(highest[2] - 1), sep="")
# Driver code
# Names of the students
names= ["sam", "ram", "geek"]
# Marks of the students
marks = [80, 79, 75]
# Updates that are to be done
updates = [0, 5, -9]
# Number of students
n = len(marks)
nameRank(names, marks, updates, n)
# This code is contributed by SHUBHAMSINGH10
C#
// C# implementation of the approach
using System;
class GFG{
public class Student
{
public string name;
public int marks, prev_rank;
public Student()
{
this.marks = 0;
this.prev_rank = 0;
}
}
// Function to print the name of student who
// stood first after updation in rank
static void nameRank(string []names, int []marks,
int []updates, int n)
{
// Array of students
Student []x = new Student[n];
for(int i = 0; i < n; i++)
{
x[i] = new Student();
// Store the name of the student
x[i].name = names[i];
// Update the marks of the student
x[i].marks = marks[i] + updates[i];
// Store the current rank of the student
x[i].prev_rank = i + 1;
}
Student highest = x[0];
for(int j = 1; j < n; j++)
{
if (x[j].marks >= highest.marks)
highest = x[j];
}
// Print the name and jump in rank
Console.Write("Name: " + highest.name +
", Jump: " +
Math.Abs(highest.prev_rank - 1));
}
// Driver code
public static void Main(string[] args)
{
// Names of the students
string []names = { "sam", "ram", "geek" };
// Marks of the students
int []marks = { 80, 79, 75 };
// Updates that are to be done
int []updates = { 0, 5, -9 };
// Number of students
int n = marks.Length;
nameRank(names, marks, updates, n);
}
}
// This code is contributed by rutvik_56
Javascript
输出:
Name: ram, Jump: 1