复旦大学2020计算机考研机试题目解答

题目引用

A题目:

#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;


int vecsum(vector<int> vec){
    int sum = 0;
    for(auto i : vec){
        sum += i;
    }
    return sum;
}

int main(){
    int count;
    cin >> count;
    vector<vector<int>> vec(count,vector<int>(5,0));
    vector<int> res(count,0);
    for(int i = 0; i < count; i++){
        for(int j = 0; j < 5; j++){
            cin >> vec[i][j];
        }
    }

    bool flag = false;

    for(int p = 0; p < count; p++){
        flag = false;
        for(int i = 0; i < 3; i++){
            for(int j = i + 1; j < 4; j++){
                for(int k = j + 1; k < 5; k++){
                    if((vec[p][i]+vec[p][j]+vec[p][k])%10 == 0){
                        res[p] = (vecsum(vec[p]) - (vec[p][i]+vec[p][j]+vec[p][k]))%10;
                        flag = true;
                        break;
                    }
                }
                if(flag) break;
            }
            if(flag) break;
            else res[p] = -1;
        }
    }    
        
    for(auto i : res) cout << i << endl;
    system("pause");
    return 0;
}

B题目:

#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;

int main(){
    int count, step;
    cin >> count >> step;
    vector<int> vec(count,0);
    for(int i = 0; i < count; i++) cin >> vec[i];
    sort(vec.begin(),vec.end());
    if(vec[vec.size() - 1] - vec[0] < step){
        cout << 0;
        return 0;
    }
    int res = 1;
    int preCur = 0;
    for(unsigned int i = 1; i < vec.size(); i++){
        if(vec[i] - vec[preCur] >= step){
            res++;
            preCur = i;
        } 
    }

    cout << res;
    system("pause");
    return 0;
}

C题目:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<vector<int>> arr(n + 1,vector(3,0));
    vector<int> res(n + 1,0);    // 从 1 开始计数
    for(int i = 1; i <= n; i++){
        cin >> arr[i][0] >> arr[i][1] >>arr[i][2];
    }

    int p = 0;
    for(int i = 1; i <= n; i++){
        int dao;
        dao = res[p] + arr[p][1] < arr[i][0] ? arr[i][0] - res[p] - arr[p][1] : 0;   // 当前来的人和上一个人之间是否有空闲
        res[i] = res[p] + arr[p][1] - arr[i][0] > arr[i][2] ? -1 : res[p] + arr[p][1] + dao;    // 超时为 -1 ,不超时计算
        if(res[i] != -1) p = i;   // 上一个打上饭同学的下标
    }

    for(int i = 1; i <= n ; i++) cout << res[i] <<" ";
    system("pause");
    return 0;
}

D题目:

#include <iostream>
#include <vector>
using namespace std;


// 使用的是LeetCode中对树的定义
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
 
int main()
{
    int count;   // 输入数量
    cin >> count;
    vector<int> vec(count + 1,0);    // 0 作为假根,从 1 开始计数
    vector<int> res(count + 1,0);  
    for(int i = 1; i <= count; i++) cin >> vec[i];
    TreeNode* source = new TreeNode(0);    // 搞一个“0”的假根
    TreeNode* father = source;
    TreeNode* root = new TreeNode(vec[1]);
    father->right = root;
    for(int n = 2; n <= count;  n++){   // 每次都去遍历一次已经建好的树
        TreeNode* t = new TreeNode(vec[n]);
        TreeNode* cur = root;
        father = source;
        int breech = 0;
        while(true){
            if(!cur) break;   // 如果当前位置没有元素,则vec[n]应该放在这个地方
            if(vec[n] > cur->val){   // 进入右子树
                father = cur;
                cur = cur->right;
                breech = 2;    // 分支标志
            } else{    // 进入左子树
                father = cur;   
                cur = cur->left;
                breech = 1;
            } 
        }
        if(breech == 1) father->left = t;
        if(breech == 2) father->right = t; 
        res[t->val] = father->val;   // 顺便记录父节点信息
    }
    for(int i = 1; i <= count; i++) cout << res[i] <<" ";
    //system("pause");    
    return 0;
}

相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页