网易互娱 8.7笔试 代码记录

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;
    }
}

原文链接: https://www.cnblogs.com/Waldeinsamkeit/p/15116095.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    网易互娱 8.7笔试 代码记录

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/212852

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 上午1:27
下一篇 2023年2月13日 上午1:28

相关推荐