当前位置:   article > 正文

小红书Java后端笔试题目(2023-08-19)_小红书 后端 笔试

小红书 后端 笔试

1. 水仙花数

#include <iostream>
#include <vector>
#include <math.h>

using namespace std;

int main() {
    int m, n;
    while (cin >> m >> n) {
        vector<int> res;
        for (int i = m; i <= n; i++) {
            int tmp = i;
            int sum = 0;
            while (tmp != 0) {
                sum += pow(tmp % 10, 3);
                tmp = tmp / 10;
            }
            if (sum == i) {
                res.push_back(i);
            }
        }
        if (res.size() == 0) {
            cout << "no" << endl;
        }
        else {
            for (int i = 0; i < res.size(); i++) {
                cout << res[i] << " ";
            }
            cout << endl;
        }
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

2. 小红的回文串

#include<iostream>
#include<string>

using namespace std;

int main() {
    int n;
    cin >> n;
    string str;
    for (int i = 0; i < n; i++) {
        cin >> str;
        int left = 0;
        int right = str.size() - 1;
        while (left < right) {
            if (str[left] == str[right]) {
                left++;
                right--;
            }
            else if ((str[left] == 'b' && str[right] == 'd') ||
                (str[left] == 'd' && str[right] == 'b') ||
                (str[left] == 'p' && str[right] == 'q') ||
                (str[left] == 'q' && str[right] == 'p') ||
                (str[left] == 'b' && str[right] == 'q') ||
                (str[left] == 'q' && str[right] == 'b') ||
                (str[left] == 'd' && str[right] == 'p') ||
                (str[left] == 'p' && str[right] == 'd') ||
                (str[left] == 'n' && str[right] == 'u') ||
                (str[left] == 'u' && str[right] == 'n')) {
                left++;
                right--;
            }
            else if (str[left] == 'w' && str[right] == 'v') {
                str[left] = 'v';
                left++;
                str.insert(str.begin() + left, 'v');
                // right 不需要减了
            }
            else if (str[left] == 'm' && str[right] == 'n') {
                str[left] = 'n';
                left++;
                str.insert(str.begin() + left, 'n');
                // right 不需要减了
            }
            else if (str[left] == 'v' && str[right] == 'w') {
                str[right] = 'v';
                str.insert(str.begin() + right, 'v');
                //right 不需要减了
                left++;
            }
            else if (str[left] == 'n' && str[right] == 'm') {
                str[right] = 'n';
                str.insert(str.begin() + right, 'n');
                //right 不需要减了
                left++;
            }
            else {
                cout << "NO" << endl;
                break;
            }
        }
        if (left >= right)
            cout << "YES" << endl;
    }

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

3. 小红的旅游策略

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

struct Node {
    int attractId;
    int transportTime;
};

int main() {
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> attractVal(n);
    for (int i = 0; i < n; i++) {
        cin >> attractVal[i];
    }

    vector<int> visitTime(n);
    for (int i = 0; i < n; i++) {
        cin >> visitTime[i];
    }

    vector<vector<Node>> nodesTime(n, vector<Node>());

    int u, v, w;
    for (int i = 0; i < m; i++) {
        cin >> u >> v >> w;
        Node node;
        node.attractId = v;
        node.transportTime = w;
        nodesTime[u].push_back(node);
    }

    int maxVal = -1;
    for (int i = 0; i < n; i++) {
        int sumTime = 0;
        sumTime += visitTime[i];
        if (sumTime > k) {
            sumTime -= visitTime[i];
            continue;
        }
        for (int j = 0; j < nodesTime[i].size(); j++) {
            sumTime += nodesTime[i][j].transportTime;
            sumTime += visitTime[nodesTime[i][j].attractId];
            if (sumTime > k) {
                sumTime -= nodesTime[i][j].transportTime;
                sumTime -= visitTime[nodesTime[i][j].attractId];
                continue;
            }
            for (int p = 0; p < nodesTime[nodesTime[i][j].attractId].size(); p++) {
                sumTime += nodesTime[nodesTime[i][j].attractId][p].transportTime;
                sumTime += visitTime[nodesTime[nodesTime[i][j].attractId][p].attractId];
                if (sumTime > k) {
                    sumTime -= nodesTime[nodesTime[i][j].attractId][p].transportTime;
                    sumTime -= visitTime[nodesTime[nodesTime[i][j].attractId][p].attractId];
                    continue;
                }
                else {
                    int curVal = attractVal[i] + attractVal[nodesTime[i][j].attractId] + attractVal[nodesTime[nodesTime[i][j].attractId][p].attractId];
                    if (curVal > maxVal) {
                        maxVal = curVal;
                    }
                }
                sumTime -= nodesTime[nodesTime[i][j].attractId][p].transportTime;
                sumTime -= visitTime[nodesTime[nodesTime[i][j].attractId][p].attractId];
            }
            sumTime -= nodesTime[i][j].transportTime;
            sumTime -= visitTime[nodesTime[i][j].attractId];
        }
        sumTime -= visitTime[i];
    }
    cout << maxVal;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/475219
推荐阅读
相关标签
  

闽ICP备14008679号