浙江大学计算机与软件学院2021年考研复试上机模拟练习

文章目录

  • 7-1 Square Friends (20 分)
  • 7-2 One Way In, Two Ways Out (25 分)
  • 7-3 Preorder Traversal (25 分)
  • 7-4 Load Balancing (30 分)

7-1 Square Friends (20 分)

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int main(){
    vector<pair<int, int>> res;
    int n, max_a;
    cin >> n >> max_a;
    for (int i = 1; i <= max_a; i++){
        int b = pow(i*1000, 0.5);
        while(b*b/1000 < i){
            b++;
        }
        while(b*b/1000 == i){
            bool fri = true;
            for (int j = 1; j < n; j++){
                if ((b+j)*(b+j)/1000 != i+j){
                    fri = false;
                    break;
                }
            }
            if (fri){
                res.push_back(make_pair(i, b));
            }
            b++;
        }
    }
    if (res.empty()){
        cout << "No Solution.";
    }else{
        for (auto i : res){
            printf("%d %d\n", i.first, i.second);
        }
    }
    return 0;
}

7-2 One Way In, Two Ways Out (25 分)

#include 
#include 
#include 
using namespace std;
int main(){
    int n, k;
    cin >> n >> k;
    vector<int> d;
    for (int i = 0; i < n; i++){
        int num;
        cin >> num;
        d.push_back(num);
    }
    for (int i = 0; i < k; i++){
        deque<int> query;
        vector<int> v;
        for (int j = 0; j < n; j++){
            int num;
            cin >> num;
            v.push_back(num);
        }
        query.push_back(d.front());
        int rank = 1;
        bool yes = true;
        for (int j = 0; j < n; j++){
            while(query.empty() || (query.front() != v[j] && query.back()  != v[j])){
                query.push_back(d[rank++]);
                if (rank == n+1){
                    yes = false;
                    break;
                }
            }
            if (query.front() == v[j]){
                query.pop_front();
            }else if (query.back()  == v[j]){
                query.pop_back();
            }
            if (!yes) break;
        }
        if (query.empty()){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }
    return 0;
}

7-3 Preorder Traversal (25 分)

#include 
#include 
#include 
using namespace std;
class TreeNode{
public:
    int val;
    TreeNode *lc, *rc;
    TreeNode(int v){
        val = v;
        lc = rc = nullptr;
    }
};
class Tree{
public:
    vector<int> post, in, pre;
    TreeNode* root;
    Tree(vector<int> post, vector<int> in){
        this->post = post;
        this->in = in;
        root = build(0, 0, post.size());
    }
    TreeNode* build(int posi_p, int posi_i, int length){
        if (length==0) return nullptr;
        TreeNode* cur = new TreeNode(post[posi_p+length-1]);
        int rank = 0;
        for (; rank < length; rank++){
            if (in[posi_i + rank] == post[posi_p+length-1]) break;
        }
        cur->lc = build(posi_p, posi_i, rank);
        cur->rc = build(posi_p+rank, posi_i+rank+1, length-rank-1);
        return cur;
    }
    void pre_traverse(TreeNode* cur){
        if (!cur) return;
        pre.push_back(cur->val);
        pre_traverse(cur->lc);
        pre_traverse(cur->rc);
    }
};
int main(){
    int n;
    cin >> n;
    vector<int> post, in;
    for (int i = 0; i < 2; i++){
        for (int j = 0; j < n; j++){
            int tmp;
            cin >> tmp;
            if (i==0){
                post.push_back(tmp);
            }else{
                in.push_back(tmp);
            }
        }
    }
    Tree t(post, in);
    t.pre_traverse(t.root);
    cout << t.pre.back();
    return 0;
}

7-4 Load Balancing (30 分)

#include 
#include 
#include 
#include 
#include 
using namespace std;
class Partition{
public:
    int m=0, diff=INT_MAX;
    void partition(vector<int> loads){
        for (int i = 0; i <loads.size(); i++){
            if (can_partition(i, loads)){
                int original = loads[i];
                for (int j = 1; j <=original/2; j++){
                    loads[i] = original-j;
                    int maximum = *max_element(loads.begin(), loads.end());
                    if (j > maximum/2){
                        loads.push_back(j);
                        partition(loads);
                        loads.pop_back();
                    }
                }
                loads[i] = original;
            }
        }
        if (loads.size() > m){
            m = loads.size();
            int M = *max_element(loads.begin(), loads.end());
            int m = *min_element(loads.begin(), loads.end());
            diff = M-m;
        }else if(loads.size() == m){
            int M = *max_element(loads.begin(), loads.end());
            int m = *min_element(loads.begin(), loads.end());
            diff = min(diff, M-m);
        }
    }
    bool can_partition(int rank, vector<int> loads){
        int s = loads[rank];
        vector<int> tmp = loads;
        int a = s/2;
        int b = s-a;
        tmp[rank] = a;
        tmp.push_back(b);
        int half = (*max_element(tmp.begin(), tmp.end())/2);
        return min(a, b) > half;
    }
};

int main(){
    cin.tie(0);
    int s;
    cin >> s;
    Partition p;
    p.partition({s});
    cout << p.m << " " << p.diff;
    return 0;
}

你可能感兴趣的:(c++,算法,数据结构)