ZISUOJ 2022年算法基础公选课练习四(Map)

本文介绍了博主通过C++和STL中的map数据结构解决的六个问题,包括数字和单词的统计、众数、不重复输出、最高分数学生姓名以及水果分配的排序问题,旨在提升算法基础和C++编程能力。
摘要由CSDN通过智能技术生成

说明:

        博主为了提早预习数据结构和C++的一些知识,自己琢磨外加查阅资料所写的代码,题目来源于22年初的学院老师组织的算法基础公选课的练习。我的代码甚至思路肯定存在许多不足和错误,欢迎大家批评指正。

题目列表:

问题 A: 数据排序——统计数字 

思路:

        开一个map<int,int>的map,键对应出现的数字,值对应键的值出现的次数,一旦出现一个数,则把该数的出现次数加1,最后遍历输出即可。值得注意的是,map默认是从小到大排列的,如果是string类型,那么默认是按字典序从小到大排列的,这跟它内部的实现有关。

参考题解:
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
map<int,int> mp;
void solve(){
    int n;cin >> n;
    int num;
    while(n--){
    	cin >> num;
    	mp[num]++;
	}
	for(auto &it:mp) cout << it.first << ' ' << it.second << endl;
}
signed main(){
	//问题 A: 数据排序——统计数字
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_--){
        solve();
    }
    return 0;
}

问题 B: c++和stl入门——数单词

 

思路:

        同理,开一个map<string,int>的map,键对应出现的单词,值对应该单词出现的次数,一旦出现一个单词,则把该单词的出现次数加1,最后遍历输出即可。

参考题解:
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
map<string,int> mp;
void solve(){
    int n;cin >> n;
    string s;
    while(n--){
    	cin >> s;
    	mp[s]++;
	}
	int maxn=0;string maxs;
	for(auto &it:mp){
		if(it.second>maxn){
			maxn = it.second;
			maxs = it.first;
		}
	}
	cout << maxs << endl;
}
signed main(){
	//问题 B: c++和stl入门——数单词
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_--){
        solve();
    }
    return 0;
}

问题 C: 数据排序——众数

思路:

        与A题思路相同,只不过输出的时候只输出出现次数最多的数(可能不止1个)和出现次数。

参考题解:
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;
map<int,int> mp;
void solve(){
	int n;cin >> n;
	int num;
	while(n--){
		cin >> num;
		mp[num]++;
	}
	int maxn=0;
	for(auto &it:mp) if(it.second>maxn) maxn=it.second;
	for(auto &it:mp) if(maxn==it.second) cout << it.first << "  " << it.second << endl;
}
int main(){
	//问题 C: 数据排序——众数
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int T=1;
//	cin >> T;
	while(T--) solve();
	return 0;
}

问题 D: 不重复地输出数

思路:

        其实这个题用set做更合适、直观一些,但是放在map的习题里面,那我们就用map做吧。同理,出现过的数字就输出,出现多次的只输出一次即可。

参考题解:
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
map<int,int> mp;
void solve(){
    int n;cin >> n;
    int num;
    while(n--){
    	cin >> num;
    	mp[num]++;
	}
	for(auto &it:mp) cout << it.first << ' ';
	cout << endl;
}
signed main(){
	//问题 D: 不重复地输出数
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_--){
        solve();
    }
    return 0;
}

问题 E: 最高分数的学生姓名

思路:

        开一个map<string,int>,键存放学生的名字,值存放其的成绩,然后遍历map名字打擂台查找最高分,然后再遍历一遍map输出最高分的名字。

参考题解:
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
map<string,int> mp;
void solve(){
    int n,score;cin >> n;
    string name;
    while(n--){
    	cin >> score >> name;
		mp[name] = score;
	}
	int maxscore = 0;
	for(auto &it:mp) if(it.second>maxscore) maxscore = it.second;
	for(auto &it:mp) if(it.second==maxscore) cout << it.first << endl;
}
signed main(){
	//问题 E: 最高分数的学生姓名
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_--){
        solve();
    }
    return 0;
}

问题 F: 水果

思路:

        这题好在我在期末考前刷到过,但是当时没学map没写出来,期末考的时候用结构体数组加sort()函数写出来了,现在回过头看看这个题用map来写是真的很简单。map根据字符串按字典序从小到大排序的特点,我们无需对内部数据再进行排序了,直接读入,然后遍历输出即可。

参考题解:
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
map<string,map<string,int> > mp;
void solve(){
    int n,num;cin >> n;
    string fruit,province;
    mp.clear();
    while(n--){
    	cin >> fruit >> province >> num;
		mp[province][fruit] += num;
	}
    for(auto &it1:mp){
    	cout << it1.first << endl;
    	for(auto &it2:it1.second) cout << "   |----" << it2.first << '(' << it2.second << ')' << endl;
	}
    cout << endl;
}
signed main(){
	//1332: 水果
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--){
        solve();
    }
    return 0;
}
  • 24
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Beau_Will

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值