程序設計與算法(三)第九周測驗(2018春季)
阿新 • • 發佈:2018-05-20
gif AS 自己的 AC 其他人 greate comm play man
題目網址:http://cxsjsxmooc.openjudge.cn/2018t3springw9/
【1:Set】
用multiset記錄當前整數集數據信息
用set記錄曾被加入集合的數
1 #include <iostream> 2 #include <set> 3 #include <string> 4 using namespace std; 5 6 7 int main() 8 { 9 10 multiset<int> s; 11 set<int> is; 12 string str;編程題 1:Set13 int n,value; 14 s.clear(); 15 cin>>n; 16 while(n--){ 17 cin>>str>>value; 18 if(str=="add"){ 19 s.insert(value); 20 cout<<s.count(value)<<endl; 21 is.insert(value); 22 } 23 else if(str=="ask"){ 24 if(is.count(value)){25 cout<<1<<" "; 26 } 27 else{ 28 cout<<0<<" "; 29 } 30 cout<<s.count(value)<<endl; 31 } 32 else if(str=="del"){ 33 cout<<s.count(value)<<endl; 34 s.erase(value); 35 } 36} 37 return 0; 38 }
【2:熱血格鬥場】
lower_bound(power)返回 第一個 元素(map裏的元素)的key值大於等於power值 的叠代器
1 #include <iostream> 2 #include <map> 3 #include <string> 4 using namespace std; 5 6 7 int main() 8 { 9 map<int, int> m; 10 m.insert(make_pair(1000000000, 1)); 11 int n; 12 cin >> n; 13 while (n--) { 14 int id, power; 15 cin >> id >> power; 16 map<int, int>::iterator i; 17 i=m.lower_bound(power); 18 if (i == m.begin()) { 19 cout << id << " " << i->second << endl; 20 } 21 else { 22 map<int, int>::iterator p = i--; 23 if (power - i->first <= p->first - power) { 24 cout << id << " " << i->second << endl; 25 } 26 else { 27 cout << id << " " << p->second << endl; 28 } 29 } 30 m.insert(make_pair(power, id)); 31 } 32 return 0; 33 }編程題 2:熱血格鬥場
【3:冷血格鬥場】
註意到相同實力值的人中,只有id號最小的那個人有可能與新人比賽,其他人可以當做不存在。
因此,這樣實力值便各不相同,就仍用map即可
故需在考慮是否在map中插入新值:若新人的實力值已存在,則該實力值對應的新id為min{新人的id,該實力值當前的id};
1 #include <iostream> 2 #include <map> 3 #include <string> 4 using namespace std; 5 6 7 int main() 8 { 9 map<int, int> m; 10 m.insert(make_pair(1000000000, 1)); 11 int n; 12 cin >> n; 13 while (n--) { 14 int id, power; 15 cin >> id >> power; 16 map<int, int>::iterator i; 17 i = m.lower_bound(power); 18 if (i == m.begin()) { 19 cout << id << " " << i->second << endl; 20 if (power != i->first) { 21 m.insert(make_pair(power, id)); 22 } 23 else { 24 if (id<i->second) { 25 m[i->first] = id; 26 } 27 } 28 } 29 else { 30 map<int, int>::iterator p = i--; 31 if (power - i->first < p->first - power) { 32 cout << id << " " << i->second << endl; 33 m.insert(make_pair(power, id)); 34 } 35 else if (power - i->first > p->first - power) { 36 cout << id << " " << p->second << endl; 37 if (power != p->first) { 38 m.insert(make_pair(power, id)); 39 } 40 else { 41 if (id<p->second) { 42 m[p->first] = id; 43 } 44 } 45 } 46 else { 47 cout << id << " "; 48 if (i->second < p->second) { 49 cout << i->second << endl; 50 } 51 else { 52 cout << p->second << endl; 53 } 54 m.insert(make_pair(power, id)); 55 } 56 } 57 } 58 return 0; 59 }編程題 3:冷血格鬥場
【4:編程填空:數據庫內的學生信息】
【提示】
1、根據以下兩句:
MyMultimap<string,int> mp;
MyMultimap<int,string,less<int> > mp2;
這意味著模板類應有3個typename或class,並且第三個有默認值
又根據 Print(mp.begin(),mp.end()); //按 姓名從大到小 輸出
應寫一個自己的greater<typename>模板
2、
根據cout << * first
應該寫一個
template <typename T1, typename T2>
ostream& operator << (ostream& os, pair<T1, T2> i) ;
內容根據輸出要求來寫
3、
根據
mp.Set("Tom",78); //把所有名為"Tom"的學生的成績都設置為78
Set函數用 multimap的equal_range函數來實現
4、
MyMultimap<string,int>::iterator p = mp.find(name);
//這裏::雙冒號說明是在自定義的類中再定義一個類,class iterator
但可以發現iterator其實代表std::multimap<key,value>::iterator
故在自己寫的MyMultimap類中寫一句
typedef typename multimap<T1, T2>::iterator iterator;
即可
本題也可參考博客https://blog.csdn.net/iamiman/article/details/53375357
1 template<class T> 2 struct myGreater { 3 bool operator() (const T& x, const T& y) const { return x>y; } 4 }; 5 6 template <typename T1, typename T2,typename pred=myGreater<T1> > 7 class MyMultimap { 8 public: 9 multimap<T1, T2,pred > m; 10 typedef typename multimap<T1, T2>::iterator iterator; 11 MyMultimap() { 12 m.clear(); 13 } 14 void clear() { 15 m.clear(); 16 } 17 void insert(pair<T1, T2> p) { 18 m.insert(make_pair(p.first, p.second)); 19 } 20 typename map<T1, T2>::iterator begin() { 21 return m.begin(); 22 } 23 typename map<T1, T2>::iterator end() { 24 return m.end(); 25 } 26 void Set(T1 key, T2 value) { 27 typename map<T1, T2>::iterator i = m.equal_range(key).first; 28 typename map<T1, T2>::iterator i2 = m.equal_range(key).second; 29 while(i!=i2){ 30 i->second=value; 31 i++; 32 } 33 } 34 typename multimap<T1, T2>::iterator find(T1 key){ 35 return m.find(key); 36 } 37 }; 38 39 template <typename T1, typename T2> 40 ostream& operator << (ostream& os, pair<T1, T2> i) { 41 os << "(" << i.first << "," << i.second << ")"; 42 return os; 43 }4:編程填空:數據庫內的學生信息
程序設計與算法(三)第九周測驗(2018春季)