京东2019届校招实习笔试题题解

京东2019届校招实习笔试题题解

题目表述:

第一题:找匹配的奇偶数

这里写图片描述

第二题:找删除字符后变成回文字符串的方案组合(有点意思)

这里写图片描述

第三题:象棋马走日方案组合

这里写图片描述
这里写图片描述

解题思路及代码:

第一题:

这题就不写了,蛮简单的,除以2除到是奇数就可以了。

第二题:

笔试的时候没有想出来,后来别人发的答案,用的DP去做的,蛮有意思的。最小子结构是一个字符,然后扩展的时候考虑是否字符一样,通过这个来传递状态。

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

int main()
{
    string s;
    cin>>s;
    int n = s.size();

    vector<vector<long> > dp;
    for(int i = 0; i<100; i++)
        dp.push_back(vector<long>(100, 0)); // 初始化dp矩阵为0
    //init
    for (int i = 0; i < n; i++){
        dp[i][i] = 1;
        if (i+1<n && s[i] == s[i+1])
            dp[i][i+1] = 3;
        else
            dp[i][i+1] = 2;
    }
    //dp update
    for (int p = 2; p < n; ++p){
        for (int i = 0, j = p; j < n; ++i, ++j)
            if (s[i] == s[j])
                dp[i][j] = dp[i+1][j] + dp[i][j-1] + 1;//加1加的是不去头不去尾,中间去光得到空串的可能性。
            else
                dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1];//减去的是重叠部分重复计算的可能性
    }
    cout<<dp[0][n-1]<<endl;
}

第三题:

据说暴力也能AC,直接提交输出0也可以50%。我想到的方法是递归,用步数–做结束递归的标志,递归的时候要注意判断是否越界。

#include <iostream>
using namespace std;
struct point
{
    int x;
    int y;
    int deadline;
};
long long ans = 0;
int end_x, end_y;
void digui(point pos){
    if (pos.deadline == 0 && pos.x == end_x && pos.y == end_y)
    {
        ans++;
        return;
    }
    //1
    if (pos.x - 1 >= 0 && pos.y - 2 >= 0){
        point temp;
        temp.x = pos.x - 1;
        temp.y = pos.y - 2;
        temp.deadline = pos.deadline-1;
        if (temp.deadline < 0)
            return;
        digui(temp);
    }
    //2
    if(pos.x - 2 >= 0 && pos.y - 1 >= 0){
        point temp;
        temp.x = pos.x - 2;
        temp.y = pos.y - 1;
        temp.deadline = pos.deadline - 1;
        if (temp.deadline < 0)
            return;
        digui(temp);
    }
    //3
    if(pos.x - 2 >= 0 && pos.y + 1 <= 8){
        point temp;
        temp.x = pos.x - 2;
        temp.y = pos.y + 1;
        temp.deadline = pos.deadline - 1;
        if (temp.deadline < 0)
            return;
        digui(temp);
    }
    //4
    if(pos.x - 1 >= 0 && pos.y + 2 <= 8){
        point temp;
        temp.x = pos.x - 1;
        temp.y = pos.y + 2;
        temp.deadline = pos.deadline - 1;
        if (temp.deadline < 0)
            return;
        digui(temp);
    }
    //5
    if(pos.x + 1 <= 8 && pos.y + 2 <= 8){
        point temp;
        temp.x = pos.x + 1;
        temp.y = pos.y + 2;
        temp.deadline = pos.deadline - 1;
        if (temp.deadline < 0)
            return;
        digui(temp);
    }
    //6
    if(pos.x + 2 <= 8 && pos.y + 1 <= 8){
        point temp;
        temp.x = pos.x + 2;
        temp.y = pos.y + 1;
        temp.deadline = pos.deadline - 1;
        if (temp.deadline < 0)
            return;
        digui(temp);
    }
    //7
    if(pos.x + 2 <= 8 && pos.y - 1 >= 0){
        point temp;
        temp.x = pos.x + 2;
        temp.y = pos.y - 1;
        temp.deadline = pos.deadline - 1;
        if (temp.deadline < 0)
            return;
        digui(temp);
    }
    //8
    if(pos.x + 1 <= 8 && pos.y - 2 >= 0){
        point temp;
        temp.x = pos.x + 1;
        temp.y = pos.y - 2;
        temp.deadline = pos.deadline - 1;
        if (temp.deadline < 0)
            return;
        digui(temp);
    }


}
int _tmain(int argc, _TCHAR* argv[])
{
    int t;
    cin >> t;
    int i = 0;
    for (i = 0; i < t;i++){
        int k;
        cin >> k;

        cin >> end_x >> end_y;
        point start;
        start.x = 0;
        start.y = 0;
        start.deadline = k;
        digui(start);
        printf("%lld", ans%10000000007);

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u014744127/article/details/79882987