STL应用:map/multimap习题POJ-2418&&POJ-3481

POJ-2418:简单的模拟和遍历,输入避免超时采用getline,注意输出格式

#include<iostream>
#include<string>
#include<map>
using namespace std;
int main() {
	map<string, int>tree;
	int num = 0;
	string s;
	while (getline(cin,s)) {
		num++;
		tree[s]++;
	}
	for (map<string, int>::iterator it = tree.begin(); it != tree.end(); it++) {
		cout << it->first << " ";
		printf("%.4f\n", 100.0*it->second / num);
	}
	return 0;
}

POJ-3481:采用一个小技巧,因为p是优先级,因此应该让p作为键(key)便于在map中排序,而最后输出值即可

#include<iostream>
#include<map>
using namespace std;
map<int, int>mp;
void max();
void min();
int main() {
	int n;
	map<int, int>::iterator ma;
	map<int, int>::iterator mi;
	do {
		scanf("%d", &n);
		switch (n) {
		case 1: int k, p; cin >> k >> p; mp[p]=k; break;
		case 2:max(); break;
		case 3:min(); break;
		}
	} while (n);
}
void max() {
	if (mp.empty()) {
		printf("0\n");
		return;
	}
	map<int, int>::iterator ma = --mp.end();
	printf("%d\n", ma->second);
	mp.erase(ma);
	return;
}
void min() {
	if (mp.empty()) {
		printf("0\n");
		return;
	}
	map<int,int>::iterator mi = mp.begin();
	printf("%d\n", mi->second);
	mp.erase(mi);
	return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

淬炼之火

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

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

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

打赏作者

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

抵扣说明:

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

余额充值