Sort 5 results Sort introductory exercises solution to a problem

Written for: "Informatics Olympiad through a" second chapter on machine Exercise 3

Title Description

The class is given a course of transcripts, please According to their scores on the report card sorting output, if there is the same fraction press lexicographically smallest first name.
(Note: "lexicographical" concept often encounter, so remember - lexicographical order is a string that appears in the dictionary, or page number can also be understood as a string that appears in the dictionary, such as character string "apple" appears in the dictionary in the order than the "banana" earlier, the "apple" lexicographic order than the "banana" small)

Input Format

The first line contains an integer \ (the n-(0 \ the n-lt \ Le 1000) \) , represents the number of students in the class;
the next \ (n \) lines, each behavior of each student's name and his achievements, separated by a single space. Name contains only letters and no longer than \ (20 \) , a score of not more than \ (100 \) non-negative integer.

Output Format

The report card by scores sorted in descending order and outputs, each row contains the names and scores two, there is a space between.

Sample input

3
xkchen 90
zpl 100
zifeiy 60

Sample Output

zpl 100
xkchen 90
zifeiy 60

Topic analysis

Sort template structure problem.
Structure is defined as follows:

struct Student {
    char name[22];  // 名字
    int point;  // 分数
} a[1001];

Comparison function is defined as follows:

bool cmp(Student a, Student b) {
    if (a.point != b.point) return a.point > b.point;
    return strcmp(a.name, b.name) < 0;
}

But we can simplify the wording comparison function, the following cmpfunction and realize the above cmpfunction the same functionality:

bool cmp(Student a, Student b) {
    return a.point > b.point || a.point == b.point && strcmp(a.name, b.name) < 0;
}

Description: This is strcmpa string comparison functions, it is used to compare two (represented by the character array) lexicographically string.
For example, for two strings aand bis:

  • If the adictionary order than the bsmall, the strcmp(a,b)returns -1;
  • If the adictionary order than blarge, strcmp(a,b)will return 1;
  • If aand bare the same string, then strcmp(a,b)returns 0.

Complete codes are as follows:

#include <bits/stdc++.h>
using namespace std;
struct Student {
    char name[22];  // 名字
    int point;  // 分数
} a[1001];
int n;
bool cmp(Student a, Student b) {
    return a.point > b.point || a.point == b.point && strcmp(a.name, b.name) < 0;
}
int main() {
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i].name >> a[i].point;
    sort(a, a+n, cmp);
    for (int i = 0; i < n; i ++) cout << a[i].name << " " << a[i].point << endl;
    return 0;
}

On the other hand, if you have learned string, we can structure the names used to describe namethe type of set string, because stringthe type of variables can be used directly relational operators compare lexicographical, we would not need to use strcmpto compare the size of the dictionary order.
Used stringcode means to achieve the following:

#include <bits/stdc++.h>
using namespace std;
struct Student {
    string name;  // 名字
    int point;  // 分数
} a[1001];
int n;
bool cmp(Student a, Student b) {
    return a.point > b.point || a.point == b.point && a.name < b.name;
}
int main() {
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i].name >> a[i].point;
    sort(a, a+n, cmp);
    for (int i = 0; i < n; i ++) cout << a[i].name << " " << a[i].point << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/zifeiynoip/p/11450507.html