#include<iostream>#include<unordered_set>usingnamespace std;int n, m;
unordered_set<int> se;intmain(){scanf("%d%d",&n,&m);int v1 =1e9, v2;for(int i =1, x; i <= n; i ++){scanf("%d",&x);int y = m - x;if(se.count(y)){if(x > y)swap(x, y);if(x < v1) v1 = x, v2 = y;}se.insert(x);}if(v1 ==1e9)puts("No Solution");elseprintf("%d %d\n", v1, v2);}
1063 Set Similarity (25 分)
題意 :
給一堆集合(可能包含相同元素),每次詢問兩個集合,問它們的 分別去重后,公有的個數 / 總共的個數
思路 :
顯然在輸入集合的時候就要去重了
語法 :
在printf中輸出%,用"%%"即可
#include<iostream>#include<unordered_set>usingnamespace std;constint N =55;int n;
unordered_set<int> S[N];intmain(){scanf("%d",&n);for(int i =1; i <= n; i ++){int m;scanf("%d",&m);while(m --){int x;scanf("%d",&x);S[i].insert(x);}}int k;scanf("%d",&k);while(k --){int a, b;scanf("%d%d",&a,&b);int nc =0;for(auto x : S[a]) nc += S[b].count(x);int nt = S[a].size()+ S[b].size()- nc;printf("%.1lf%%\n",(double)nc / nt *100);}return0;}
1078 Hashing (25 分)
題意 :
distinct的序列插入到一個哈希表中,然后輸出每個按順序所給的輸入數字的位置
哈希函數定義為key%TSize,TSize為哈希表的最大大小
利用只具有正增量的二次探測法來解決沖突Quadratic probing (with positive increments only) is used to solve the collisions.
#include<iostream>usingnamespace std;constint N =1e4+10;int s, n;int h[N];boolis_prime(int x){if(x ==1)returnfalse;for(int i =2; i * i <= x; i ++)if(x % i ==0)returnfalse;returntrue;}intfind(int x){int t = x % s;for(int k =0; k < s; k ++){int i =(t + k * k)% s;if(!h[i])return i;}return-1;}intmain(){scanf("%d%d",&s,&n);while(!is_prime(s)) s ++;int x;for(int i =0; i < n &&scanf("%d",&x); i ++){int k =find(x);if(k ==-1)printf("-");else{h[k]= x;printf("%d", k);}if(i != n -1)printf(" ");}}
1120 Friend Numbers (20 分)
思路 :什么數據結構可以讓我們既判重又排序呢?其實不用哈希表而用set會更好,可以少一步排序
語法 :set默認從小到大排序;空格輸出新方法(這題如果不判空格會wa)
#include<iostream>#include<set>usingnamespace std;intmain(){int n;cin >> n;set<int> se;while(n --){int x;cin >> x;int s =0;while(x) s += x %10, x /=10;se.insert(s);}cout << se.size()<< endl;;bool is_first =true;for(auto x : se){if(is_first) is_first =false;else cout <<' ';cout << x;}return0;}
#include<iostream>#include<algorithm>#include<vector>#include<unordered_map>#include<cmath>usingnamespace std;structStudent{string id;int p, m, f, s;Student():p(-1),m(-1),f(-1),s(0){}voidcalc(){if(m > f) s =round(m *0.4+ f *0.6);else s = f;}booloperator<(const Student &t)const{if(s != t.s)return s > t.s;return id < t.id;}};intmain(){int P, M, N;scanf("%d%d%d",&P,&M,&N);unordered_map<string, Student> hash;string id;int grade;for(int i =0; i < P; i ++){cin >> id >> grade;hash[id].id = id;hash[id].p = grade;}for(int i =0; i < M; i ++){cin >> id >> grade;hash[id].id = id;hash[id].m = grade;}for(int i =0; i < N; i ++){cin >> id >> grade;hash[id].id = id;hash[id].f = grade;}vector<Student> students;for(auto&item : hash){auto&stu = item.second;stu.calc();if(stu.p >=200&& stu.s >=60)students.push_back(stu);}sort(students.begin(), students.end());for(int i =0; i < students.size(); i ++){auto&stu = students[i];cout << stu.id <<' '<< stu.p <<' '<< stu.m <<' '<< stu.f <<' '<< stu.s << endl;}}
#include<iostream>usingnamespace std;constint N =10010;int s, n, m;int h[N];boolis_prime(int x){if(x ==1)returnfalse;for(int i =2; i * i <= x; i ++)if(x % i ==0)returnfalse;returntrue;}intfind(int x,int&cnt){int t = x % s;cnt =1;for(int k =0; k < s; k ++, cnt ++){int i =(t + k * k)% s;if(!h[i]|| h[i]== x)return i;}return-1;}intmain(){cin >> s >> n >> m;while(!is_prime(s)) s ++;for(int i =0; i < n; i ++){int x, count;cin >> x;int t =find(x, count);if(t ==-1)printf("%d cannot be inserted.\n", x);else h[t]= x;}int cnt =0;for(int i =0; i < m; i ++){int x, count;cin >> x;find(x, count);cnt += count;}printf("%.1lf\n",(double)cnt / m);return0;}
1149 Dangerous Goods Packaging (25 分)
題意 :
本題給定一張不相容物品的清單,需要你檢查每一張集裝箱貨品清單,判斷它們是否能裝在同一只箱子里。
思路 :
對于每個詢問,只要檢查每一對關系即可,因此,只需要開兩個數組存儲
#include<iostream>#include<unordered_set>usingnamespace std;constint N =1e4+10;int n, m;int a[N], b[N];intmain(){scanf("%d%d",&n,&m);for(int i =0; i < n; i ++)scanf("%d%d",&a[i],&b[i]);int cnt;while(m --){scanf("%d",&cnt);unordered_set<int> se;int x;while(cnt --){scanf("%d",&x);se.insert(x);}bool success =true;for(int i =0; i < n; i ++)if(se.count(a[i])&& se.count(b[i])){success =false;break;}if(!success)puts("No");elseputs("Yes");}}