北京大学程序设计MOOC作业详解-09-标准模板库STL(二)

北京大学程序设计MOOC作业详解-09-标准模板库STL

直接上代码,有问题请留言。

第一题:

#include <iostream>
#include <set>
#include <cstdio>
#include <cstring>
using namespace std;

int main() {
    int T;
    scanf("%d", &T);
    multiset<int> numSet;
    set<int> numVis;
    while (T--) {
        char cmd[5] = {'\0'};
        scanf("%s", cmd);
        int x;
        scanf("%d", &x);
        if (!strcmp("add", cmd)) {
            numSet.insert(x);
            printf("%d\n", numSet.count(x));
            numVis.insert(x);
        }
        else if (!strcmp("del", cmd)) {
            printf("%d\n", numSet.count(x));
            numSet.erase(x);
        }
        else if (!strcmp("ask", cmd)) {
            if (numVis.count(x) > 0) {
                printf("1 %d\n", numSet.count(x));
            }
            else {
                printf("0 0\n");
            }
        }
    }
    return 0;
}

第二题:

#include <cstdio>
#include <queue>
#include <cmath>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

const int INF = 0x3f3f3f3f;
struct VIP {
    int id;
    int power;

    VIP(int _id, int _power) : id(_id), power(_power) { }

    bool operator<(const VIP& rhs) const {
        return id < rhs.id;
    }
};

int main() {
    vector<VIP> vips;
    vips.emplace_back(1, 1000000000);
    int n, curId, curPower;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d %d", &curId, &curPower);
        vips.emplace_back(curId, curPower);
    }
    map<int, int> mpOfPowerToIdx;
    map<int, int>::iterator iterOfPowerToIdx;
    mpOfPowerToIdx[vips[0].power] = 0;
    for (int i = 1; i <= n; ++i) {
        curPower = vips[i].power;
        int morePower = INF, moreIdx = -1;
        int lessPower = INF, lessIdx = -1;
        iterOfPowerToIdx = mpOfPowerToIdx.lower_bound(curPower);
        morePower = iterOfPowerToIdx->first;
        moreIdx = iterOfPowerToIdx->second;
        if (iterOfPowerToIdx == mpOfPowerToIdx.end()) {
            lessIdx = mpOfPowerToIdx.rbegin()->second;
            printf("%d %d\n", vips[i].id, vips[lessIdx].id);
        }
        else if (iterOfPowerToIdx == mpOfPowerToIdx.begin()) {
            printf("%d %d\n", vips[i].id, vips[moreIdx].id);
        }
        else {
            --iterOfPowerToIdx;
            lessPower = iterOfPowerToIdx->first;
            lessIdx = iterOfPowerToIdx->second;
            int disMore = abs(vips[i].power - morePower);
            int disLess = abs(vips[i].power - lessPower);
            if (disMore < disLess) {
                printf("%d %d\n", vips[i].id, vips[moreIdx].id);
            }
            else {
                printf("%d %d\n", vips[i].id, vips[lessIdx].id);
            }
        }
        mpOfPowerToIdx[vips[i].power] = i;
    }
    return 0;
}

第三题:

#include <cstdio>
#include <map>
#include <cmath>
#include <climits>
using namespace std;

int main() {
    int n, power, id;
    map<int, int> vips;// power->id
    map<int, int>::iterator updateIt, solveIt;
    scanf("%d", &n);
    vips[1000000000] = 1;
    for (int i = 1; i <= n; ++i) {
        scanf("%d %d", &id, &power);
        solveIt = vips.lower_bound(power);
        int dis1 = abs(power - solveIt->first), id1 = solveIt->second;
        int dis2 = INT_MAX, id2 = -1;
        if (solveIt != vips.begin()) {
            --solveIt;
            dis2 = abs(power - solveIt->first), id2 = solveIt->second;
        }
        if (dis1 == dis2) {
            int oldId = (id1 < id2) ? id1 : id2;
            printf("%d %d\n", id, oldId);
        }
        else {
            if (dis1 < dis2) {
                printf("%d %d\n", id, id1);
            }
            else {
                printf("%d %d\n", id, id2);
            }
        }
        updateIt = vips.find(power);
        if (updateIt == vips.end()) {
            vips.insert(make_pair(power, id));
        }
        else if (updateIt->second > id) {
            updateIt->second = id;
        }
    }
    return 0;
}

第四题:

#include <iostream>
#include <string>
#include <map>
#include <iterator>
#include <algorithm>
using namespace std;
template<class K, class V, class Pred = greater<K>>
class MyMultimap : public multimap<K, V, Pred> {
public:
    void Set(K key, V value) {
        using Iter = typename multimap<K, V, Pred>::iterator;
		Iter st = this->lower_bound(key);
		Iter ed = this->upper_bound(key);
		for (; st != ed; ++st) {
			st->second = value;
		}
    }
};

template<class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}
struct Student 
{
	string name;
	int score;
};
template <class T>
void Print(T first,T last) {
	for(;first!= last; ++ first)
		cout << * first << ",";
	cout << endl;
}
int main()
{
	
	Student s[] = { {"Tom",80},{"Jack",70},
					{"Jone",90},{"Tom",70},{"Alice",100} };
	
	MyMultimap<string,int> mp;
	for(int i = 0; i<5; ++ i)
		mp.insert(make_pair(s[i].name,s[i].score));
	Print(mp.begin(),mp.end()); //按姓名从大到小输出

	mp.Set("Tom",78); //把所有名为"Tom"的学生的成绩都设置为78
	Print(mp.begin(),mp.end());
	
	
	
	MyMultimap<int,string,less<int> > mp2;
	for(int i = 0; i<5; ++ i) 
		mp2.insert(make_pair(s[i].score,s[i].name));
	
	Print(mp2.begin(),mp2.end()); //按成绩从小到大输出
	mp2.Set(70,"Error");          //把所有成绩为70的学生,名字都改为"Error"
	Print(mp2.begin(),mp2.end());
	cout << "******" << endl;
	
	mp.clear();
	
	string name;
	string cmd;
	int score;		
	while(cin >> cmd ) {
		if( cmd == "A") {
			cin >> name >> score;
			if(mp.find(name) != mp.end() ) {
				cout << "erroe" << endl;
			}
			mp.insert(make_pair(name,score));
		}
		else if(cmd == "Q") {
			cin >> name;
			MyMultimap<string,int>::iterator p = mp.find(name);
			if( p!= mp.end()) {
				cout << p->second << endl;
			}
			else {
				cout << "Not Found" << endl; 
			}		
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值