B1004. Ranking (20)

Topic description:

Read in the names, student IDs, and grades of n students, and output the names and student IDs of the students with the highest and lowest grades, respectively.

Input format: Each test input contains 1 test case in the format

  Line 1: positive integer n
  Row 2: The first student's name, student number, grades
  Row 3: The name and student number of the second student
  ... ... ...
  Line n+1: The name and student number of the nth student
The name and student ID are both strings of no more than 10 characters, and the grade is an integer between 0 and 100. It is guaranteed that no two students have the same grade in a set of test cases.

Output format: output 2 lines for each test case, the first line is the name and student ID of the student with the highest grade, the second line is the name and student ID of the student with the lowest grade, and there is a space between the strings.

Input sample:
3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95
Sample output:
Mike CS991301
Joe Math990112

Ideas:

(1) Use the structure to store the student data, use the score as the subscript (there is no situation where the student scores are the same), and record the maximum and minimum values ​​while reading the data.

        Then output the required information directly; use the variables max and min to record the maximum and minimum values ​​respectively, and assign -1,101 respectively to facilitate updating;

        Use strcpy() to copy the string. To add the header file #include <cstring>.

(2) Three structure variables of temp, max and min can be directly defined for reading in data and storing the maximum and minimum values;

(1) The code is as follows:

#include <cstdio>
#include <cstring>

struct student
{
	char name[15];
	char id[15];
	int score;
}	st[110];

intmain()
{
	int n, max = -1, min = 101; //As long as there is data input, it will be updated;
	char name[15], id[15];
	int score;
	
	scanf ("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf ("%s%s%d", name, id, &score);
		strcpy (st[score].name, name); //Copy characters into the structure;
		strcpy (st[score].id, id);
		if (score >= max) max = score; //Record the highest score;
		if (score <= min) min = score; //Record the lowest score
	}
	
	printf ("%s %s\n", st[max].name, st[max].id);
	printf ("%s %s\n", st[min].name, st[min].id);
 	return 0;
}

(2) The code is as follows:

#include <cstdio>

struct student
{
	char name[15];
	char id[15];
	int score;
}temp, max, min;

intmain()
{
	int n;
	scanf ("%d", &n);
	max.score = -1;
	min.score = 101;
	for (int i = 0; i < n; i++)
	{
		scanf ("%s%s%d", temp.name, temp.id, &temp.score);
		if (temp.score > max.score) max = temp; //The structure can be assigned directly; 
		if (temp.score < min.score)     min = temp;
	}
	
	printf ("%s %s\n", max.name, max.id);
	printf ("%s %s\n", min.name, min.id);

 	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324370701&siteId=291194637