当前位置:网站首页>Zstuacm registration results (complete with STL linked list)

Zstuacm registration results (complete with STL linked list)

2022-07-23 06:02:00 Rain and cold at night in Xiaoxiang

Title Description
Teacher Ye wants to register grades with a linked list . There are two messages for each test paper : Student id and grade .
For registration results , It is required that grades with a small student number be registered before grades with a large student number .
Teacher Ye has two operations :
1 a b: Add the student number to the linked list a The result is b Of the students , The title ensures that different papers will not have the same student number .
2 k : Query the... In the current linked list k What are the grades of students , Title assurance k Less than or equal to the length of the current linked list .
Please use the linked list to do this problem !
Input
Enter an integer in the first line Q, Represents the number of operations .
Next input Q That's ok , Each line is (1 a b) perhaps (2 k) Format , Represents the first and second operations respectively .
1 <= k, Q <= 1000
1 <= a <= 1000000000
1 <= b <= 100
Output
For each second operation , Output an integer to represent the k The results of three students
The sample input Copy
3
1 20163266 100
1 20163265 99
2 1
Sample output Copy
99
Tips
Please use a linked list .

Everyone knows the linked list , When I was a freshman, I took a bunch of special Abstract pointers to complete , At that time, I was confused for a long time
( After all, I have felt that there is no pressure on the content of the class for a long time. Suddenly, there is an instant that can't be solved, and the mentality is not very good )
Because the pointer is used , Many students may have a headache when they see pointers or linked lists .
But thank you c++ Added in STL Standard library , Let the code of the linked list become very concise and easy to understand , The best thing is STL The future is available in most cases , This is for novice Xiaobai , Very friendly

**#include <iostream>
#include<cstdio>
#include<algorithm>
#include<list>
using namespace std;
typedef struct students// Define a structure for storing data
{
long long num;
int score;
};
bool cmp(students stu1,students stu2)
// This is list Of sort Required in function , The students are arranged in ascending order //
{
bool flag;
if (stu1.num < stu2.num)
{
return true;
}
else
{
return false;
}
}
int main()
{
int n;
cin >> n;
long long num1;
int search, score1;

list<students>stu1;
struct students stu2;

list<students>::iterator lit;// Pointer to iterator 

int flag = 0;
for (int i = 0; i < n; i++)
{
    cin >> flag;
    if (flag == 1)
    {
        cin >> stu2.num >> stu2.score;
        stu1.push_front(stu2);
    }
    else
    {
        cin >> search;
        stu1.sort(cmp);//list Medium sort function 
        lit = stu1.begin();
        for (int j = 1; j < search; j++)
        {
            lit++;
        }
        cout << lit->score << endl;
    }
}
return 0;

}**

原网站

版权声明
本文为[Rain and cold at night in Xiaoxiang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207221756480230.html