科软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;
}

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

相关推荐
董董灿是个攻城狮2 小时前
AI视觉连载8:传统 CV 之边缘检测
算法
哈里谢顿3 小时前
1000台裸金属并发创建中的重难点问题分析
面试
哈里谢顿3 小时前
20260303面试总结(全栈)
面试
over6978 小时前
从 LLM 到全栈 Agent:MCP 协议 × RAG 技术如何重构 AI 的“做事能力”
面试·llm·mcp
SuperEugene9 小时前
Vue状态管理扫盲篇:如何设计一个合理的全局状态树 | 用户、权限、字典、布局配置
前端·vue.js·面试
blasit9 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
AI软著研究员9 小时前
程序员必看:软著不是“面子工程”,是代码的“法律保险”
算法
FunnySaltyFish10 小时前
什么?Compose 把 GapBuffer 换成了 LinkBuffer?
算法·kotlin·android jetpack
颜酱11 小时前
理解二叉树最近公共祖先(LCA):从基础到变种解析
javascript·后端·算法
Sailing11 小时前
🚀 别再乱写 16px 了!CSS 单位体系已经进入“计算时代”,真正的响应式布局
前端·css·面试