网易互娱21.4.18笔试代码
1. 乒乓球比赛
#include <cstdio> #include <cstdlib> #include <string> #include <cstring> #include <vector> #include <queue> #include <stack> #include <map> #include <algorithm> #include <cmath> #include <iostream> using namespace std; int arr[9][3][2] = { {{0, 1}, {2, 3}, {4, 5}}, {{0, 1}, {2, 4}, {3, 5}}, {{0, 1}, {2, 5}, {3, 4}}, {{0, 2}, {1, 3}, {4, 5}}, {{0, 2}, {1, 4}, {3, 5}}, {{0, 2}, {1, 5}, {3, 4}}, {{1, 2}, {0, 3}, {4, 5}}, {{1, 2}, {0, 4}, {3, 5}}, {{1, 2}, {0, 5}, {3, 4}}, }; int main() { int T; scanf("%d", &T); while(T--){ int A[6], B[6]; for(int i=0; i<6; i++) scanf("%d", &A[i]); for(int i=0; i<6; i++) scanf("%d", &B[i]); int ans = 0; for(int i=0; i<9; i++){ for(int j=0; j<9; j++){ int cnt = 0; for(int k =0; k<3; k++){ int temp_a, temp_b = 0; temp_a = A[arr[i][k][0]] + A[arr[i][k][1]]; temp_b = B[arr[j][k][0]] + B[arr[j][k][1]]; if(temp_b > temp_a) cnt ++; } if(cnt >=2) ans ++; } } printf("%d\n", ans); } return 0; }
2. 猜拳,求最大存活人数
#include <cstdio> #include <cstdlib> #include <string> #include <cstring> #include <vector> #include <queue> #include <stack> #include <map> #include <algorithm> #include <cmath> #include <iostream> using namespace std; int main() { int T; scanf("%d", &T); while(T--){ int N, M; scanf("%d%d", &N, &M); char str[N]; scanf("%s", str); // cin >> str; int ans = 0; for(int i=0; i< N; i++){ int cur = i; int m = M; int n = N; string s = str; // cout << s.size(); while(m--){ if(s.size() <= 1) break; int nex = (cur+1)%n; if(s[cur] == s[nex]) cur = nex; else if((s[cur] == 'R' && s[nex] == 'S') || (s[cur] == 'S' && s[nex] == 'C') || (s[cur] == 'C' && s[nex] == 'R')){ s.erase(nex, 1); cur = nex < cur ? cur-1 : cur; n--; } else{ s.erase(cur, 1); cur = nex < cur ? nex : cur; n--; } } ans = max(ans, n); } printf("%d\n", ans); } return 0; }
3. 游戏排行榜,中位数送礼物
#include <cstdio> #include <cstdlib> #include <string> #include <cstring> #include <vector> #include <queue> #include <stack> #include <map> #include <set> #include <algorithm> #include <cmath> #include <iostream> using namespace std; void Insert(vector<int> &rank, int s){ vector<int>::iterator it= rank.begin(); // 边界问题想不动了,干脆hard coding了.... if(rank.size()==0){ rank.push_back(s); return; } if(s <= rank[0]){ rank.insert(it, s); return; } for(it = rank.begin(); (it+1)!=rank.end() && (it)!=rank.end(); it++) if(*(it) <= s && *(it+1) >=s){ rank.insert(it+1, s); return; } rank.insert(it+1, s); } int main() { int T; scanf("%d", &T); while(T--){ int N, id, s; scanf("%d", &N); vector<int> rank; set<int> gift; map<int, int> id_s; for(int i=0; i<N; i++){ scanf("%d%d", &id, &s); map<int, int>::iterator mpit = id_s.find(id); if(mpit != id_s.end() && mpit->second >= s) continue; if(mpit != id_s.end()) for(vector<int>::iterator it = rank.begin(); it!=rank.end(); it++) if(*(it) == mpit->second){ rank.erase(it); break; } Insert(rank, s); int n_rank = rank.size(); if(n_rank % 2 == 1){ if( s == rank[n_rank /2]) gift.insert(id); } else{ if((rank[n_rank/2] + rank[n_rank/2 -1]) % 2 == 0 && (rank[n_rank/2] + rank[n_rank/2 -1]) / 2 == s) gift.insert(id); } id_s[id] = s; } printf("%d\n", gift.size()); } return 0; }