科软25机试

发布于:2025-03-29 ⋅ 阅读:(27) ⋅ 点赞:(0)

题目:

2025科软复试上机题(回忆版)题解_哔哩哔哩_bilibili

1. 字符串反转

#include<bits/stdc++.h>
using namespace std;

void solve(string& a, int CurN) {
    if (!(CurN % 2)) {
        int right = a.size() - 1;
        int left = 0;
        while (left < right) {
            swap(a[left], a[right]);
            left++;
            right--;
        }
    }
}

void print(vector<string>& ans) {
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << " ";
    }
    cout << endl; 
}

int main() {
    string s;
    getline(cin, s);
    stringstream ss(s);
    string tmpStr;
    int count = 0;
    vector<string> ans;
    while (getline(ss, tmpStr, ' ')) {
        ans.push_back(tmpStr);
    }
    for (int i = 0; i < ans.size(); i++) {
        solve(ans[i], i + 1);
    }
    print(ans);
    return 0;
}

2.进制转换 

思路:将32进制字符串转换为十进制数;将十进制数转换为八进制字符串
#include<bits/stdc++.h>
using namespace std;
unordered_map<char, int> base32ToDec = {
    {'0', 0}, {'1', 1}, {'2', 2}, {'3', 3}, {'4', 4}, {'5', 5}, {'6', 6}, {'7', 7},
    {'8', 8}, {'9', 9}, {'A', 10}, {'B', 11}, {'C', 12}, {'D', 13}, {'E', 14}, {'F', 15},
    {'G', 16}, {'H', 17}, {'I', 18}, {'J', 19}, {'K', 20}, {'L', 21}, {'M', 22}, {'N', 23},
    {'O', 24}, {'P', 25}, {'Q', 26}, {'R', 27}, {'S', 28}, {'T', 29}, {'U', 30}, {'V', 31}
};
int solve1(string& Str32) {
    int decimalValue = 0;
    int length = Str32.length();
    for (int i = 0; i < length; ++i) {
        char ch = Str32[i];
        decimalValue += base32ToDec[ch] * static_cast<int>(pow(32, length - i - 1));
    }
    return decimalValue;
}
string solve2(int decimalValue) {
    string octalStr;
    while (decimalValue > 0) {
        octalStr += to_string(decimalValue % 8);
        decimalValue /= 8;
    }
    reverse(octalStr.begin(),octalStr.end());
    return octalStr.empty() ? "0" : octalStr;
}
int main() {
    string Str32; 
	getline(cin,Str32);
    int decimalValue = solve1(Str32);
    string octalStr = solve2(decimalValue);
	cout<<octalStr<<endl;
    return 0;
}

3. 区间移除 

#include<bits/stdc++.h>>
using namespace std;
int solve(vector<pair<int, int>>& arr) {
    if (arr.empty()) return 0;
    sort(arr.begin(), arr.end(), [](pair<int, int> a, pair<int, int> b) {
        return a.second < b.second; 
    });
    int count = 0; 
    int end = INT_MIN;
    for (auto& it :arr) {
        if (it.first >= end) {
            end = it.second;
        } else {
            count++;
        }
    }
    return count;
}
int main() {
    int n;
    cin >> n;
    vector<pair<int, int>> ans(n);
    for (int i = 0; i < n; i++) {
        cin >> ans[i].first >> ans[i].second;
    }
    cout << solve(ans) << endl;
    return 0;
}

4. 机器人

#include<bits/stdc++.h>
using namespace std;
int m, n;
vector<vector<int>> grid;
vector<vector<bool>> visited;
int maxValue = -1;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
void dfs(int x, int y, int sum) {
    if (x == m - 1 && y == n - 1) {
        maxValue = max(maxValue, sum);
        return;
    }
    visited[x][y] = true;
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx >= 0 && nx < m && ny >= 0 && ny < n && !visited[nx][ny] && grid[nx][ny] != -1) {
            dfs(nx, ny, sum + grid[nx][ny]);
        }
    }
    visited[x][y] = false;
}
int main() {
    cin >> m >> n;
    grid.resize(m, vector<int>(n));
    visited.resize(m, vector<bool>(n, false));
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            string value;
            cin >> value;
            if (value == "#") {
                grid[i][j] = -1; 
            } else {
                grid[i][j] = stoi(value);
            }
        }
    }
    if (grid[0][0] == -1 || grid[m-1][n-1] == -1) {
        cout << -1 << endl;
        return 0;
    }
    dfs(0, 0, grid[0][0]);
    cout << maxValue << endl;
    return 0;
}

据说这次的机试很难,由于我没有真实的上机的环境,所以完全凭借经验分享一下我的解题思路(有疑问的可以发在评论区)