美团网上笔试题

本文解析了2018年美团网上笔试中的编程题目,包括基础的计算机科学知识考核及具体的编程实现。提供了两道题目的代码示例,一道关于团队组合的简单逻辑题,另一道则为更复杂的背包问题,涉及动态规划算法。

前言

2018.9.6美团的网上笔试题,基础题目离不开计算机网络,操作系统,数据结构和数据库。这些基础学科都考了(估计大多数岗位这些题是一样的),其他题的不同取决于应聘的职位。笔试还出现了一些逻辑题和数学题。

 

这道编程题还是比较简单的,不要想的太复杂,很快就能做出来。

#include <iostream>
using namespace std;

int main()
{
    int i = 1;     //i是退出循环的标记
    int j=0;       //j用来记录能组多少队
    int n,m;       //n,m代表两类人
    cout << "Input n&m:";
    cin >> n >> m;
    while(i == 1 && m >= 1 && n>= 1)
    {
        if(m+n>=3)
        {
            m -= 1;       
            n -= 1;       //3人成一队,编程的人和算法的人都先选一个
            if(m > n)     //剩下的那一个人就从人多的里面选
                m -= 1;   
            else
                n -= 1;
            j+=1;         //组成3人队伍,队数加一
        }
        else
            i=0;
    }
    cout << "SUM:" << j <<endl;
    return 0;
}

输入:4                         输入:5
     30 20 50 50                    13 10 21 15
     10 10 20 15                    3 4 15 10
     60 80 70 83                    20 13 25 18
     40 40 50 75                    33 26 45 40
                                    60 41 78 80
输出:165                       输出:112   
                        

思路:背包问题

#include <bits/stdc++.h>
using namespace std;
int TIME = 121;
int process(const vector<int>& p, const vector<int>& a, const vector<int>& q, const vector<int>& b) 
    {
    int n = p.size();
    if (n == 0) return 0;

    vector<vector<int> > memo(n,vector<int>(TIME+1,-1));
    for (int j = 0; j <= TIME; ++j) {
        memo[0][j] = (j >= p[0] ? a[0]:0);
        memo[0][j] = (j >= q[0] && b[0] > a[0] ? b[0]:memo[0][j]);
    }

    for (int i = 1; i < n; ++i) {
        for (int j = 0; j <= TIME ; ++j) {
            memo[i][j] = memo[i-1][j];
            if (j >= p[i])
                memo[i][j] = max(memo[i][j],a[i] + memo[i-1][j-p[i]]);
            if (j >= q[i]) {
                memo[i][j] = max(memo[i][j],b[i] + memo[i-1][j-q[i]]);
            }
        }
    }
    return memo[n-1][TIME];
}

int main()
{
    int n;
    cin >> n;
    int dp[TIME];
    memset(dp, 0, TIME * sizeof(int));
    int p, a, q, b;

    vector<int> pVector;
    vector<int> aVector;
    vector<int> qVector;
    vector<int> bVector;

    for (int k = 0; k < n; ++k) {
        cin >> p >> a >> q >> b;
        pVector.push_back(p);
        aVector.push_back(a);
        qVector.push_back(q);
        bVector.push_back(b);
    }
    cout << process(pVector,aVector,qVector,bVector);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tyler_Zx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值