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

7-1 Square Friends (20 分)

#include <iostream>
#include <deque>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
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 <iostream>
#include <deque>
#include <vector>
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 <iostream>
#include <deque>
#include <vector>
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 <iostream>
#include <deque>
#include <vector>
#include <climits>
#include <algorithm>
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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值