题解 | 玩家积分榜系统

玩家积分榜系统

https://www.nowcoder.com/practice/5b5cb654caa249eb979e4be483e36c1e

#include <iostream>
#include <string>
#include <map>
using namespace std;

map<string,int> mp;

void insertOrUpdateScore(const string &name, int score)
{
    // TODO: 实现插入或更新逻辑
    mp[name]=score;
    cout<<"OK"<<endl;
    return;
}

void queryScore(const string &name)
{
    // TODO: 实现查询逻辑
    if(mp.count(name)==0){
    	cout<<"Not found"<<endl;
	}else{
		cout<<mp[name]<<endl;
	}
	return;
}

void deletePlayer(const string &name)
{
    // TODO: 实现删除逻辑
    if(mp.count(name)==0){
    	cout<<"Not found"<<endl;
	}else{
		mp.erase(name);
		cout<<"Deleted successfully"<<endl;
	}
	return;
}

void countPlayers()
{
    // TODO: 实现统计逻辑
    cout<<mp.size()<<endl;
    return;
}

int main()
{
    // 提高输入输出效率 (可选)
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int q;
    cin >> q;

    while (q--)
    {
        int op;
        cin >> op;

        if (op == 1)
        {
            string name;
            int score;
            cin >> name >> score;
            insertOrUpdateScore(name, score);
        }
        else if (op == 2)
        {
            string name;
            cin >> name;
            queryScore(name);
        }
        else if (op == 3)
        {
            string name;
            cin >> name;
            deletePlayer(name);
        }
        else if (op == 4)
        {
            countPlayers();
        }
    }

    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务