洛谷 P5266 【深基17.例6】学籍管理(Luogu p5266 [Shenji 17. Example 6] student status management)

题目描述

您要设计一个学籍管理系统,最开始学籍数据是空的,然后该系统能够支持下面的操作(不超过 10^5105 条):

  • 插入与修改,格式1 NAME SCORE:在系统中插入姓名为 NAME(由字母和数字组成不超过 20 个字符的字符串,区分大小写) ,分数为 \texttt{SCORE}SCORE(0<\texttt{SCORE}<2^{31}0
OK
  • 查询,格式2 NAME:在系统中查询姓名为 NAME 的学生的成绩。如果没能找到这名学生则输出Not found,否则输出该生成绩。
  • 删除,格式3 NAME:在系统中删除姓名为 NAME 的学生信息。如果没能找到这名学生则输出Not found,否则输出Deleted successfully。
  • 汇总,格式4:输出系统中学生数量。

输入格式

输出格式

输入输出样例

5
1 lxl 10
2 lxl
3 lxl
2 lxl
4
10
Deleted successfully
Not found
0分析map练习题代码
#include<bits/stdc++.h>
using namespace std;

int n;

map<string,long long> student;
map<string,long long>::iterator it;

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int q;
        long long num;
        cin>>q;
        string a;
        if(q==1)
        {
            cin>>a;
            cin>>num;
            it=student.find(a);
            if(it!=student.end())//找到了 
            {
                it->second=num;
            }
            else 
            {
                student.insert(pair<string,long long>(a,num));
            }
            cout<<"OK"<<endl;
        }
        else if(q==2)
        {
            cin>>a;
            it=student.find(a);
            if(it==student.end())
            {
                cout<<"Not found"<<endl;
            }
            else cout<<it->second<<endl; 
        }
        else if(q==3)
        {
            cin>>a;
            int f=student.erase(a);
            if(f) cout<<"Deleted successfully"<<endl;
            else cout<<"Not found"<<endl;
        }
        else if(q==4)
        {
            cout<<student.size()<<endl;
        }
    }
    return 0;
}
————————

Title Description

You want to design a student status management system. At first, the student status data is empty, and then the system can support the following operations (no more than)   10^5105   Item:

  • Insert and modify, format 1 name score: insert name (a string composed of letters and numbers, no more than 20 characters, case sensitive) into the system, and the score is  \ Students of texttt {score} score (0 < \ texttt {score} < 2 ^ {31} 0 < score < 231). If there is already a student with the same name, update the student's grade to score. If the insertion or modification is successful, the output is OK.
OK
  • Query, format 2 Name: query the scores of students with name in the system. If the student cannot be found, output not found, otherwise output the student’s score.
  • Delete, format 3 Name: delete the student information with name in the system. If the student cannot be found, output not found; otherwise, output deleted successfully.
  • Summary, format 4: output the number of students in the system.

Input format

nothing

Output format

nothing

Input and output samples

5
1 lxl 10
2 lxl
3 lxl
2 lxl
4
10
Deleted successfully
Not found
0分析map练习题代码
#include<bits/stdc++.h>
using namespace std;

int n;

map<string,long long> student;
map<string,long long>::iterator it;

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int q;
        long long num;
        cin>>q;
        string a;
        if(q==1)
        {
            cin>>a;
            cin>>num;
            it=student.find(a);
            if(it!=student.end())//找到了 
            {
                it->second=num;
            }
            else 
            {
                student.insert(pair<string,long long>(a,num));
            }
            cout<<"OK"<<endl;
        }
        else if(q==2)
        {
            cin>>a;
            it=student.find(a);
            if(it==student.end())
            {
                cout<<"Not found"<<endl;
            }
            else cout<<it->second<<endl; 
        }
        else if(q==3)
        {
            cin>>a;
            int f=student.erase(a);
            if(f) cout<<"Deleted successfully"<<endl;
            else cout<<"Not found"<<endl;
        }
        else if(q==4)
        {
            cout<<student.size()<<endl;
        }
    }
    return 0;
}