1:Set
#include<iostream> #include<set> #include<string> using namespace std; int main() { int n; cin >> n; multiset<int> ms; set<int> s; multiset<int>::iterator pp; set<int>::iterator p; for( int i = 0; i < n; i++ ) { string op; cin >> op; int a; cin >> a; switch(op[1]) { case 'd': // add ms.insert(a); s.insert(a); cout << ms.count(a) << endl; break; case 's': // ask p = s.find(a); if( p != s.end() ) cout << 1 << " "; else cout << 0 << " "; cout << ms.count(a) << endl; break; case 'e': // del cout << ms.count(a) << endl; ms.erase(ms.lower_bound(a), ms.upper_bound(a)); break; default: break; } } return 0; }
2:热血格斗场
#include<iostream> #include<set> using namespace std; #define MAX 1000000000 struct member{ int id; int ce; }; struct rule{ bool operator()( const member &a1, const member &a2 ){ return (a1.ce < a2.ce); } }; int main() { int n; cin >> n; member a; set<member, rule> st; set<member, rule>::iterator p, p1, p2; a.id = 1; a.ce = MAX; st.insert(a); for( int i = 0; i < n; i++ ) { scanf("%d%d", &a.id, &a.ce); st.insert(a); p1 = p2 = p = st.find(a); p2++; if( p1 != st.begin() ) p1--; if( p2 == st.end() ) p2--; if( p1 != p && p2 != p ) if( p->ce - p1->ce <= p2->ce - p->ce ) printf("%d %d\n", p->id, p1->id); else printf("%d %d\n", p->id, p2->id); else if( p1 == p && p2 != p ) printf("%d %d\n", p->id, p2->id); else if( p1 != p && p2 == p ) printf("%d %d\n", p->id, p1->id); } return 0; }
3:冷血格斗场
#include<iostream> #include<map> using namespace std; #define MAX 1000000000 struct member{ int ce, id; // first, second }; int id_LeftFindMin( multimap<int, int>::iterator p1 ) { member a; a.id = p1->second; a.ce = p1->first; while( (--p1)->first == a.ce ) if( p1->second < a.id ) a.id = p1->second; return a.id; } int id_RightFindMin( multimap<int, int>::iterator p2 ) { member a; a.id = p2->second; a.ce = p2->first; while( (++p2)->first == a.ce ) if( p2->second < a.id ) a.id = p2->second; return a.id; } int main() { int n; cin >> n; multimap<int, int> mp; // int first -- int ce, int second -- int id member a; a.id = 1; a.ce = MAX; mp.insert(make_pair(a.ce, a.id)); multimap<int, int>::iterator p, p1, p2; for( int i = 0; i < n; i++ ) { scanf("%d%d", &a.id, &a.ce); p = p1 = p2 = mp.insert(make_pair(a.ce, a.id)); if( p1 != mp.begin() ) p1--; if( ++p2 == mp.end() ) p2--; if( p1 != p && p2 != p ){ if( p->first - p1->first < p2->first - p->first ) a.id = id_LeftFindMin(p1); else if( p->first - p1->first > p2->first - p->first ) a.id = id_RightFindMin(p2); else{ // p->first - p1->first == p2->first - p->first p1->second = id_LeftFindMin(p1); p2->second = id_RightFindMin(p2); a.id = (p1->second < p2->second) ? p1->second : p2->second; } } else if( p1 == p && p2 != p ) a.id = id_RightFindMin(p2); else if( p1 != p && p2 == p ) a.id = id_LeftFindMin(p1); printf("%d %d\n", p->second, a.id); } return 0; }
4:编程填空:数据库内的学生信息
#include <iostream> #include <string> #include <map> #include <iterator> #include <algorithm> using namespace std; template< class Key, class Val, class Pred = greater<Key> > class MyMultimap: public multimap<Key,Val,Pred> { public: void Set(Key key, Val value) { typedef typename multimap<Key, Val, Pred>::iterator myIt; // pair<myIt,myIt> bounds = this->equal_range(key); // for( myIt it = bounds.first; it != bounds.second; it++ ) // it->second = value; // 或者下面这种写法 for( myIt it = this->find(key); it->first == key; it++ ) it->second = value; } }; template<class T1, class T2> ostream & operator<<( ostream & os, 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; }