科软25机试

题目:

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

1. 字符串反转

cpp 复制代码
#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.进制转换

cpp 复制代码
思路:将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. 区间移除

cpp 复制代码
#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. 机器人

cpp 复制代码
#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;
}

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

相关推荐
evans在进步1 小时前
LeetCode 64:最小路径和——Java 原地动态规划详解
java·leetcode·动态规划
豆角焖肉1 小时前
SSM 聚合项目搭建:多 Maven 模块项目
开发语言·python·pycharm
Frank_refuel1 小时前
【C++八股】面向对象
开发语言·c++
h_a_o777oah2 小时前
【图论】Tarjan 缩点:解决有向图中环的问题
c++·算法·图论·acm·强连通分量·缩点·tarjan
Tisfy2 小时前
LeetCode 1386.安排电影院座位:哈希表+位运算
算法·leetcode·散列表·题解·哈希表
Nil2082 小时前
leetcode 199二叉树的右视图
算法·leetcode·深度优先
m0_380743873 小时前
为 OpenAI 兼容接口配置教程
开发语言·python·node.js
M78佐菲3 小时前
c语言学习笔记:排序与查找方法整理
linux·c语言·笔记·学习·算法
万邦科技Lafite3 小时前
阿里巴巴拍立淘按图搜索商品API返回值实践:提升用户购物满意度的关键措施
开发语言·api·开放api·电商开放平台·京东开放平台
别动我齐刘海4 小时前
机器人运动控制学习4——状态估计 State Estimation
c++·人工智能·学习·目标检测·机器学习·机器人·自动驾驶