小红书笔试题目(2023-08-19)

1. 水仙花数(AC)

#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;
}

2. 小红的回文串(AC)

#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;
}

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;
}
全部评论

相关推荐

孤寡孤寡的牛牛很热情:为什么我2本9硕投了很多,都是简历或者挂,难道那个恶心人的测评真的得认真做吗
点赞 评论 收藏
分享
11-15 19:28
已编辑
蚌埠坦克学院 硬件开发
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务