14、华为OD面试题
1、找座位
在一个大型体育场举办的活动,要求至少间隔一个空位才可落座。给出一排观众座位分布图,计算在不移动现有观众座位的情况下,最多还能坐下多少名观众。输入是一个数组,0表示该座位没有坐人,1表示该座位已经坐人。输出是一个整数,表示最多还能坐下多少名观众。

#include <iostream> #include <string> #include <vector> #include <thread> #include <memory> using namespace std; int main(void) { int res = 0; std::string str; std::cin >> str; int len = str.size(); int i = 0; bool left = false; bool right = false; while (i<len) { if (str[i] == '0') { if (i == 0 || str[i - 1] == '0') { left = true; } if (i == len - 1 || str[i + 1] == '0') { right = true; } if (left == true && right == true) { res++; } } i++; left = false; right = false; } std::cout << res << std::endl; }
2、拼接url
给定一个url前缀和url后缀,通过",“分割,需要将其连接为一个完整的url,如果前缀结尾和后缀开头都没有”/“,需要自动补上”/“连接符,如果前缀结尾和后缀开头都为”/",需要自动去重。
约束:不用考虑前后缀URL不合法情况

#include <iostream> #include <string> #include <vector> #include <thread> #include <memory> using namespace std; int main(void) { const char character = '/'; std::string url1; std::string url2; std::cin >> url1; std::cin >> url2; if (!url1.empty() && url1.front() != character) { url1.insert(0, 1, character); } if (!url2.empty() && url2.back() != character) { url2.push_back(character); } if (!url1.empty() && !url2.empty() && url1.back() != character && url2.front() != character) { url1.push_back(character); } if (!url1.empty() && !url2.empty() && url1.back() == character && url2.front() == character) { url2.erase(0, 1); } std::string str = url1 + url2; std::cout << str << std::endl; }
3、给定一个数组,编写一个函数来计算它的最大N个数与最小N个数的和。你需要对数组进行去重

#include <iostream> #include <vector> #include <algorithm> #include <unordered_set> // 计算最大N个不重叠数与最小N个不重叠数的和 int maxMinSum(std::vector<int>& nums, int N) { // 如果数组为空或者N的值大于数组的大小,则返回0 if (nums.empty() || N > nums.size()) { return 0; } // 对数组进行排序 std::sort(nums.begin(), nums.end()); // 去重 std::unordered_set<int> uniqueNums(nums.begin(), nums.end()); nums.assign(uniqueNums.begin(), uniqueNums.end()); std::sort(nums.begin(), nums.end()); // 计算最大N个不重叠数的和 int maxSum = 0; int size = nums.size(); for (int i = size - 1; i >= size - N && i >= 0; --i) { maxSum += nums[i]; } // 计算最小N个不重叠数的和 int minSum = 0; for (int i = 0; i < N && i < size; ++i) { minSum += nums[i]; } // 返回最大N个数与最小N个数的和 return maxSum + minSum; } int main() { std::vector<int> nums = { 3, 1, 4, 1, 5, 9, 2, 6, 3, 4 }; int N = 3; int result = maxMinSum(nums, N); std::cout << "最大" << N << "个不重叠数与最小" << N << "个不重叠数的和为:" << result << std::endl; return 0; }
4、在学校中 N个小朋友站成一队 第i个小朋友的身高为height[i] 第i个小朋友可以看到第一个比自己身高更高的小朋友j 那么j是i的好朋友 (要求:j > i) 请重新生成一个列表 对应位置的输出是每个小朋友的好朋友的位置 如果没有看到好朋友 请在该位置用0代替 小朋友人数范围 0 ~ 40000

#include <iostream> #include <vector> #include <stack> std::vector<int> findGoodFriends(const std::vector<int>& heights) { int N = heights.size(); std::vector<int> result(N, 0); // 初始化结果数组,全部填充为0 std::stack<int> st; // 单调递减栈,存储小朋友的索引 for (int i = 0; i < N; ++i) { while (!st.empty() && heights[st.top()] < heights[i]) { result[st.top()] = i + 1; // 找到好朋友,记录好朋友的位置 st.pop(); } st.push(i); // 当前小朋友入栈 } return result; } int main() { std::vector<int> heights = { 3, 6, 7, 2, 5, 4, 8, 1 }; // 小朋友的身高数组 std::vector<int> result = FindGoodFriends(heights); std::cout << "每个小朋友的好朋友位置列表:" << std::endl; for (int i = 0; i < result.size(); ++i) { std::cout << "小朋友" << i + 1 << "的好朋友位置:" << result[i] << std::endl; } return 0; }
5、给你一个字符串 s,字符串s首尾相连成一个环形 ,请你在环中找出 'o' 字符出现了偶数次最长子字符串的长度。

#include <iostream> #include <string> #include <vector> int maxEvenOSubstringLength(std::string s) { // 复制字符串并连接成环 std::string sCircle = s + s; int maxLength = 0; // 遍历每个位置作为子字符串的起始位置 for (int i = 0; i < s.length(); i++) { int count = 0; // 向前遍历统计 'o' 字符出现的次数 for (int j = i; j < i + s.length(); j++) { if (sCircle[j] == 'o') { count++; } // 如果 'o' 字符出现了偶数次,则更新最长子字符串的长度 if (count % 2 == 0) { maxLength = std::max(maxLength, j - i + 1); } } } return maxLength; } int main() { std::string s = "looxdolx"; int maxLen = maxEvenOSubstringLength(s); std::cout << "环中 'o' 字符出现了偶数次的最长子字符串的长度为:" << maxLen << std::endl; return 0; }
6、给定一个字符串s,最多只能进行一次变换,返回变换后能得到的最小字符串

#include <iostream> #include <string> #include <algorithm> std::string MinAfterSwap(std::string& str) { std::string str_sort(str.begin(), str.end()); std::sort(str_sort.begin(), str_sort.end()); for (int i = 0; i < str.size(); ++i) { if (str[i] != str_sort[i]) { for (int j = str.size() - 1; j >= 0; j--) { if (str_sort[i] == str[j]) { std::swap(str[i], str[j]); break; } } break; } } return str; } int main() { std::string temp = "fedcba"; std::string str = MinAfterSwap(temp); std::cout << "变换后得到的最小字符串:" << str << std::endl; return 0; }
7、题目描述
给定一个字符串,只包含大写字母,求在包含同一字母的子串中,长度第k长的子串的长度,相同字母只取最长的那个子串。
输入描述
第一行有一个子串(1<长度<=100),只包含大写字母
第二行为k的值
输出描述
输出连续出现次数第k多的字母的次数

#include <iostream> #include <string> #include <algorithm> int main() { std::string str = "AAAAHHHBBCDHHHH"; int k = 3; int i = 0; int j = 0; int index = 0; int count = 0; int cnts[26] = { 0 }; int size = str.size(); while (i < size) { j = i; while (str[j] == str[i] && j< size) { j++; } index = str[i] - 'A'; count = j - i; cnts[index] = std::max(cnts[index], count); i = j; } std::sort(cnts, cnts + 26, [](int a, int b) { return a > b; }); if (k > 26 || cnts[k - 1] == 0) { std::cout << "-1" << std::endl; } else { std::cout << cnts[k - 1] << std::endl; } return 0; }
8、机械绘图

#include <iostream> #include <string> #include <algorithm> int main() { int n = 0;; std::cin >> n; int e = 0; std::cin >> e; int last_x = 0; int start_y = 0; int area = 0; for (int i = 0; i < n; i++) { int x = 0; std::cin >> x; int y = 0; std::cin >> y; area += (x - last_x) * std::abs(start_y); start_y += y; last_x = x; } area += (e - last_x) * std::abs(start_y); std::cout << area << std::endl; return 0; }
9、最大坐标值
小明在玩一个游戏,游戏规则如下:
在游戏开始前,小明站在坐标轴原点处(坐标值为0).
给定一组指令和一个幸运数,每个指令都是一个整数,小明按照指令前进指定步数或者后退指定步数。前进代表朝坐标轴的正方向走,后退代表朝坐标轴的负方向走。
幸运数为一个整数,如果某个指令正好和幸运数相等,则小明行进步数+1。
例如:
幸运数为3,指令为[2,3,0,-5]
指令为2,表示前进2步;
指令为3,正好和幸运数相等,前进3+1=4步;
指令为0,表示原地不动,既不前进,也不后退。
指令为-5,表示后退5步。
请你计算小明在整个游戏过程中,小明所处的最大坐标值。

#include <iostream> #include <string> #include <vector> #include <algorithm> #include <sstream> int main() { int nums = 0; std::cin >> nums; int luck_num = 0; std::cin >> luck_num; int max_value = 0; int curr_value = 0; for (int i = 0; i < nums; i++) { int value; std::cin >> value; if (value == luck_num) { if (value > 0) { value += 1; } else { value -= 1; } } curr_value += value; max_value = std::max(curr_value, max_value); } std::cout << max_value << std::endl; return 0; }
10、题目描述:
现有两组服务器A和B,每组有多个算力不同的CPU,其中 A[i] 是A组第i个CPU的运算能力,B[i]是 B组 第i个CPU的运算能力。
一组服务器的总算力是各CPU的算力之和。
为了让两组服务器的算力相等,允许从每组各选出一个CPU进行一次交换。 求两组服务器中,用于交换的CPU的算力,并且要求从A组服务器中选出的CPU,算力尽可能小。

#include <iostream> #include <vector> #include <unordered_map> #include <climits> #include <algorithm> using namespace std; int main() { int Num_A = 0; int Num_B = 0; int Sum_A = 0; int Sum_B = 0; std::cin >> Num_A >> Num_B; std::vector<int> A; std::unordered_map<int, int> B; for (int i = 0; i < Num_A; i++) { int value; std::cin >> value; A.push_back(value); Sum_A += value; } for (int i = 0; i < Num_B; i++) { int value; std::cin >> value; B[i] = value; Sum_B += value; } int temp = std::abs(Sum_A - Sum_B); temp /= 2; std::sort(A.begin(), A.end()); for (auto iter : A) { int target = std::abs(iter - temp); for (int i = 0; i < Num_B; i++) { if (target == B[i]) { std::cout << iter << ":" << target << std::endl; return 0; } } } return 0; }
11、题目描述
有位客人来自异国,在该国使用m进制计数。该客人有个幸运数字n(n<m),每次购物时,其总是喜欢计算本次支付的花费(折算为异国的价格后)中存在多少幸运数字。问:当其购买一个在我国价值k的产品时,其中包含多少幸运数字?
输入描述
第一行输入为 k, n, m。
其中:
k 表示 该客人购买的物品价值(以十进制计算的价格)
n 表示 该客人的幸运数字
m 表示 该客人所在国度的采用的进制
输出描述
输出幸运数字的个数,行末无空格。当输入非法内容时,输出0 作者:算法大师-刷题ing https://www.bilibili.com/read/cv29465392/ 出处:bilibili

#include <iostream> #include <vector> #include <unordered_map> #include <climits> #include <algorithm> using namespace std; int main() { int k, n, m; std::cin >> k >> n >> m; int sum = 0; while (k) { if (k % n == m) { sum++; } k = k / n; } std::cout << sum << std::endl; return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2019-04-29 C#中Socket通讯(外网通讯,借助花生壳内网穿透)