Algorithm Learning--Day1

In order to sprint for the first postgraduate exam, I am going to pick up the algorithms of the past in my spare time. Practice the algorithm questions a lot and prepare for the computer test of the algorithm in advance.

Today is April 14th, and there are still two months before the algorithm test. I have recorded all my learning in these two months here. Not only the preparation of algorithms, but also the preparation of English and projects. I hope to go to the ideal school for further study in two months.

Come on!

 

At the beginning, I started with the basic questions. Since Jiuduoj is already gg, I will transfer my position to Niuke.com, and start brushing the test questions here.

 

first question:

There are data of N students, sort the student data by grades, if the grades are the same, sort by the alphabetical order of the name characters, if the alphabetical order of the names is also the same, sort by the age of the students, and output the sorted information of the N students .

Enter description:

There are multiple groups of test data, each input has an integer N (N<=1000) in the first line, and the next N lines include the data of N students.

The data of each student includes name (a string of length not exceeding 100), age (integer), grade (positive number less than or equal to 100).

Output description:

Sort student information by grades, and if the grades are the same, sort alphabetically by name.

Then output the student information in the following format:

name, age

The alphabetical order of students' names is case-sensitive, for example, A is ahead of the alphabetical order of a (because the ASC code of A is smaller than the ASC code of a).

Example 1

enter

3

abc 20 99

bcd 19 97

bed 20 97

output

bcd 19 97

bed 20 97

abc 20 99

 

This problem is not difficult, put the code on it

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
struct E{
    char name[101];
    int age;
    int grades;
}buf[1000];

bool cmp(E a, E b){
    if (a.grades != b.grades) return a.grades < b.grades;
    int cmp = strcmp(a.name,b.name);
    if (cmp != 0){
        return cmp < 0;
    }
    return a.age<b.age;
}
int main() {
    int n;
    while (scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++){
            scanf("%s%d%d",buf[i].name,&buf[i].age,&buf[i].grades);
        }
        sort(buf,buf+n,cmp);
        for(int i=0;i<n;i++){
        printf("%s %d %d\n",buf[i].name,buf[i].age,buf[i].grades);
        }

    }
    return 0;
}

 

 

Second question:

Topic description

Use a one-dimensional array to store student numbers and grades, and then sort the output according to grades.

Enter description:

The first line of input includes an integer N (1<=N<=100), which represents the number of students.
The next N lines each include two integers p and q, representing each student's student number and grade, respectively.

Output description:

Sort students according to their grades from small to large, and print out the sorted student information.
If the grades of the students are the same, they will be sorted from small to large according to the size of the student number.
Example 1

enter

3
1 90
2 87
3 92

output

2 87
1 90
3 92


#include <stdio.h>
#include <iostream>

#include <algorithm>
using namespace std;
struct E{
    int number;
    int scores;
}buf[101];
bool cmp(E a,E b){
    if (a.scores!=b.scores) return a.scores<b.scores;
    else{
        return a.number<b.number;
    }
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        scanf("%d%d",&buf[i].number,&buf[i].scores);
    }
    sort(buf,buf+n,cmp);
    for(int i=0;i<n;i++){
    printf("%d %d\n",buf[i].number,buf[i].scores);
    }

    return 0;
}

 

Guess you like

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