京东2016笔试题

第一题 寻宝

  • 一个人在n*m迷宫寻宝,每个格子都有一个宝藏
  • 这个人只能拿k个宝藏,每次到一个点如果该点的宝藏比他手中任何一个都值钱就可以拿,也可以不拿。
  • 起点左上角终点右下角,只能向下或向右走
  • 问有多少种拿的方案。结果mod 1000000007 取余
  • 输入n,m,k,下面输入一个n*m矩阵

我的结果,深度搜索。
考试当场小毛病没改出来16%正确率…后来发现横纵坐标写反了。醉了。。。以后再也不用xy做坐标了,用row col的话就不会想歪了。

#include <iostream>
#include <vector>
using namespace std;
int dfs(vector<vector<int> >& map, int x, int y, int max, int nums, const int k, const int w, const int h) {
    if (x >= w || y >= h) return 0;
    if (x == w - 1 && y == h - 1) {
        if (nums == k) return 1;
        if (nums == k - 1 && max < map[y][x]) return 1;
        return 0;
    }
    if (nums < k) {
        int t = map[y][x];
        int sum = 0;
        if (t > max) {
            sum += dfs(map, x + 1, y, t, nums + 1, k, w, h);
            sum += dfs(map, x, y + 1, t, nums + 1, k, w, h);
            sum+= dfs(map, x, y + 1, max, nums, k, w, h);
            sum += dfs(map, x+1, y, max, nums, k, w, h);
        }
        else {
            sum += dfs(map, x + 1, y, max, nums, k, w, h);
            sum += dfs(map, x, y + 1, max, nums, k, w, h);
        }
        return sum;
    }
    else {
        int sum = 0;
        sum += dfs(map, x + 1, y, max, nums, k, w, h);
        sum += dfs(map, x, y + 1, max, nums, k, w, h);
        return sum;
    }
}
int main()
{
    int n, m, k;
    cin >> n >> m >> k;
    vector<vector<int> > map(n, vector<int>(m,0));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            int ci;
            cin >> ci;
            map[i][j] = ci;
        }
    }
    cout << dfs(map, 0, 0, -1, 0, k, m, n) % 1000000007 << endl;
    return 0;
}

第二题 花园浇水

  • 要求用喷水器给20*2m2的花园浇水
  • 给你一组花洒的半径,保证能够覆盖
  • 你求出要求完全浇到水,最少要的花洒数

输入:

5 //花洒数量
1 2 3 5.5 6 //每个半径

输出最少要的花洒数

我的解法,当时时间不够用了。后来发现蛮简单的。
cmath库勾股定理求距离就好了。。。
然后排个序,先来最大的,再接着来。。

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>
using namespace std;
int greedy(vector<float> &rn,int i,float minus) {
    float t = sqrt(pow(rn[i],2)-1);
    if (t*2 > minus) return 1;
    return greedy(rn, i + 1, minus - t*2)+1;
}
int main()
{
    int n;
    cin >> n;
    vector<float> rn(n,0.0);
    for (int i = 0; i < n; i++) {
        float a;
        cin >> a;
        rn[i] = a;
    }
    sort(rn.begin(), rn.end(),greater<float>());
    cout << greedy(rn,0,20) << endl;
    return 0;
}

京东估计跪了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值