網易互娛 8.7筆試 程式碼記錄
阿新 • • 發佈:2021-08-08
A 模擬
#include <bits/stdc++.h> using namespace std; int a[] = {0, 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; char b[] = "10X98765432"; bool islegal(string s) { string s1 = s.substr(0, 17); int sum = 0, id = 0; for (auto c : s1) sum += (c - '0') * a[++id]; sum %= 11; return b[sum] == s[17]; } int main() { int T; cin >> T; while(T--) { string s; cin >> s; char c1 = s[14], c2 = s[15], c3 = s[16]; string pre = s.substr(0, 14); int ans = 0; for (char n1 = '0'; n1 <= '9'; n1++) { if(c1 != '*' && n1 != c1) continue; for (char n2 = '0'; n2 <= '9'; n2++) { if(c2 != '*' && c2 != n2) continue; for (char n3 = '0'; n3 <= '9'; n3++) { if(c3 != '*' && n3 != c3) continue; string tmp = pre + n1 + n2 + n3 + s[17]; if(islegal(tmp)) ans++; } } } cout << ans << endl; } }
B 拓撲
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; vector<int> g[N]; int deg[N]; int main() { int T; cin >> T; while(T--) { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { g[i].clear(); deg[i] = 0; } set<int> st; while(m--) { int c; cin >> c; vector<int> v(c); for (auto &i : v) { cin >> i; st.insert(i); } for (int i = 1; i < v.size(); i++) { g[v[i]].push_back(v[i-1]); deg[v[i-1]]++; } } if(st.size() == n) { stack<int> ans; queue<int> Q; for (int i = 1; i <= n; i++) if(deg[i] == 0) Q.push(i); int num = 0; bool ok = 1; while(!Q.empty()) { if(Q.size() > 1) ok = 0; int x = Q.front(); Q.pop(); num++; ans.push(x); for (auto v : g[x]) { deg[v]--; if(deg[v] == 0) Q.push(v); } } if(ok == 1) { while(!ans.empty()) { cout << ans.top() << " "; ans.pop(); } cout << endl; } else { puts("NO"); } } else { puts("NO"); } } }
C 貪心
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 1e5 + 5; typedef pair<int,int> P; typedef long long ll; int a[N], w[N]; struct node { int a, w; node (int _a, int _w) { a = _a; w = _w; } bool operator < (const node& rhs) const { // if(w != rhs.w)return w < rhs.w; // else return a > rhs.a; return 1ll * (100 + (a+1) * w) * (100 + (rhs.a * rhs.w)) < 1ll * (100 + a * w) * (100 + (rhs.a+1) * rhs.w); } }; int main() { int T; cin >> T; while(T--) { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i] >> w[i]; w[i]--; } // max { 100 + w[i] * a[i] } priority_queue<node> Q; for (int i = 1; i <= n; i++) { Q.push(node(a[i], w[i])); } ll ans = 1; while(k && !Q.empty()) { node x = Q.top(); Q.pop(); if(x.a < 100) { x.a++; k--; Q.push(x); } else { // cout << x.w << " " << x.a << endl; ans = ans * (100 + x.w * x.a) % mod; } } while(!Q.empty()) { node x = Q.top(); Q.pop(); // cout << x.w << " " << x.a << endl; ans = ans * (100 + x.w * x.a) % mod; } cout << ans << endl; } }