程序设计实习MOOC / 程序设计与算法(一)第12周测验(2019夏季)

003:Set

#include <iostream>
#include<set>
#include<cstring>
#pragma warning(disable:4996)
using namespace std;
typedef multiset<int>::iterator MultIt;
void add(multiset<int> &MSet, set<int> &st,int num) {
	MSet.insert(num);
	st.insert(num);
	MultIt l, h;
	l = MSet.lower_bound(num);
	h = MSet.upper_bound(num);
	int count = 0;
	while (l != h) {
		count++;
		l++;
	}
	cout << count << endl;
}
void ask(const multiset<int>& MSet, const set<int>& st, int num) {
	if (st.find(num) == st.end())
		cout << 0 << ' ' << 0 << endl;
	else {
		cout << 1 << ' ';
		MultIt l, h;
		l = MSet.lower_bound(num);
		h = MSet.upper_bound(num);
		int count = 0;
		while (l != h) {
			count++;
			l++;
		}
		cout << count << endl;
	}
}
int main()
{
	//freopen("C:\\Users\\czh\\Desktop\\2.txt", "r", stdin);
	multiset<int> MSet;
	set<int> st;
	char cmd[10];
	int n;
	cin >> n;
	while (n--) {
		int num;
		cin >> cmd >> num;
		if (strcmp("add", cmd) == 0)
			add(MSet, st, num);
		else if (strcmp("del", cmd) == 0)
			cout << MSet.erase(num) << endl;
		else if (strcmp("ask", cmd) == 0)
			ask(MSet,st,num);
	}
	return 0;
}

004:热血格斗场

#include <iostream>
#include<set>
#include<cmath>
#pragma warning(disable:4996)
using namespace std;
struct Player {
	int id;
	int attack;
};
struct rule{
	bool operator()  (const Player& p1, const Player& p2) const{
		return p1.attack < p2.attack;
	}
};
typedef multiset<Player, rule>::iterator iter;
int main()
{
	//freopen("C:\\Users\\czh\\Desktop\\2.txt", "r", stdin);
	multiset<Player, rule> st;
	int n;
	cin >> n;
	Player p = {1,1000000000 };
	st.insert(p);
	while (n--) {
		cin >> p.id >> p.attack;
		cout << p.id << ' ';
		iter i;
		i = st.upper_bound(p);
		int l, h = i->attack, hid = i->id;
		if (i != st.begin()) {
			l = (--i)->attack;
			if (abs(p.attack - l) <= abs(p.attack - h))
				cout << i->id;
			else
				cout << hid;
		}
		else
			cout << hid;
		cout << endl;
		st.insert(p);
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值