洛谷 P5266 【深基17.例6】學籍管理
阿新 • • 發佈:2021-11-24
題目描述
您要設計一個學籍管理系統,最開始學籍資料是空的,然後該系統能夠支援下面的操作(不超過10^5105條):
- 插入與修改,格式
1 NAME SCORE
:在系統中插入姓名為 NAME(由字母和數字組成不超過 20 個字元的字串,區分大小寫) ,分數為\texttt{SCORE}SCORE(0<\texttt{SCORE}<2^{31}0<SCORE<231) 的學生。如果已經有同名的學生則更新這名學生的成績為 SCORE。如果成功插入或者修改則輸出OK
。 - 查詢,格式
2 NAME
:在系統中查詢姓名為 NAME 的學生的成績。如果沒能找到這名學生則輸出Not found
,否則輸出該生成績。 - 刪除,格式
3 NAME
:在系統中刪除姓名為 NAME 的學生資訊。如果沒能找到這名學生則輸出Not found
,否則輸出Deleted successfully
。 - 彙總,格式
4
:輸出系統中學生數量。
輸入格式
無
輸出格式
無
輸入輸出樣例
輸入 #15
1 lxl 10
2 lxl
3 lxl
2 lxl
4
輸出 #1OK
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;
}