当前位置: 首页 > article >正文

【华为OD:C++机试】Day-1

目录

🌷1. 统计监控、需要打开多少监控器:

🌷2. 阿里巴巴找黄金宝箱:

🌷3. 事件推送:

🌷4. 分苹果:

🌷5. 乱序整数序列两数之和绝对值最小:

🌷6.卡片组成的最大数字:

🌷7.找最小数:

🌷8.身高排序:

🌷9.出错的或电路:

🌷10.磁盘容量:



🌷1. 统计监控、需要打开多少监控器:

题目描述:

思路:

 将地图先进行存储,遍历地图,如果是1直接++,如果是0,判断四周如果是1直接++,然后break,如果在此不break则会出现多算的情况;

 code: 

// 统计监控的个数
#include <iostream>
using namespace std;

int m, n;
int grid[20][20];

int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, 1, 0, -1 };

int main()
{
	// 用于存储地图
	cin >> m >> n;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin >> grid[i][j];
		}
	}

	int ans = 0;
	for (int row = 0; row < m; row++)
	{
		for (int col = 0; col < n; col++)
		{
			if (grid[row][col] == 1)
			{
				ans++;
			}
			else
			{
				for (int i = 0; i < 4; i++)
				{
					int x = row + dx[i];
					int y = col + dy[i];

					if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1)
					{
						ans++;
						break;
					}
				}
			}
		}
	}

	cout << ans;
	return 0;
}

🌷2. 阿里巴巴找黄金宝箱:

题目描述:

code:

// 阿里巴巴寻宝箱
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	string str;
	cin >> str;

	int pos = 0;
	vector<int> v;
	while ((pos = str.find(',')) != str.npos)
	{
		v.push_back(stoi(str.substr(0, pos)));
		str.erase(0, pos + 1);
	}
	v.push_back(stoi(str));

	int leftsum = 0, rightsum = 0;
	for (auto e : v)
		rightsum += e;

	for (int i = 0; i < v.size(); i++)
	{
		rightsum -= v[i];
		if (leftsum == rightsum)
		{
			cout << i;
			return 0;
		}
		leftsum += v[i];
	}
	cout << -1;
	return 0;
}

🌷3. 事件推送:

题目描述:

code:

// 事件推送
#include <iostream>
#include <vector>
using namespace std;

void sloveMathod(int R, const vector<int>& a, const vector<int>& b)
{
	int index = 0;
	vector<vector<int>> list;

	for (int i = 0; i < a.size(); i++)
	{
		vector<int> input(2);
		while (index < b.size())
		{
			if (a[i] <= b[index] && b[index] - a[i] <= R)
			{
				input[0] = a[i];
				input[1] = b[index];
				list.push_back(input);
				break;
			}
			index++;
		}
	}

	for (const auto& e : list)
		cout << e[0] << " " << e[1] << endl;
}

int main()
{
	int m, n, R;
	cin >> m >> n >> R;

	vector<int> a(m);
	vector<int> b(n);
	for (auto& e : a) cin >> e;
	for (auto& e : b) cin >> e;

	sloveMathod(R, a, b);
	return 0;
}

🌷4. 分苹果:

题目描述:

code:

// 分苹果
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>

using namespace std;

void solveMethod(string line)
{
	// 将苹果的重量以整形的方式读入数组中
	stringstream ss(line);
	vector<int> nums;
	int n;
	while (ss >> n)
	{
		nums.push_back(n);
	}

	sort(nums.begin(), nums.end());

	int max_num = -1;

	for (int i = 1; i < nums.size() - 1; i++)
	{
		int left_bin = 0;
		int right_bin = 0;
		int left_sum = 0;
		int right_sum = 0;

		for (int j = 0; j < i; j++)
		{
			left_bin ^= nums[j];
			left_sum += nums[j];
		}

		for (int j = i; j < nums.size(); j++)
		{
			right_bin ^= nums[j];
			right_sum += nums[j];
		}

		if (left_bin == right_bin)
		{
			max_num = max(max(left_sum, max_num), right_sum);
		}
	}

	cout << max_num << endl;
}

int main()
{
	// 读入苹果的个数
	int N;
	cin >> N;
	cin.ignore();

	// 读入各苹果的重量
	string line;
	getline(cin, line);

	solveMethod(line);

	return 0;
}

🌷5. 乱序整数序列两数之和绝对值最小:

题目描述:

code:

// 乱序整数序列两数之和绝对值最小
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>

using namespace std;

void solveMethod(string line)
{
	// 将数据全部存入之nums数组中
	stringstream ss(line);
	int n;
	vector<int> nums;
	while (ss >> n)
		nums.push_back(n);

	sort(nums.begin(), nums.end());
	nums.erase(unique(nums.begin(), nums.end()), nums.end());

	int min = INT_MAX;
	vector<int> ans(2);
	for (int i = 0; i < nums.size(); i++)
	{
		for (int j = 0; j < nums.size(); j++)
		{
			int a = nums[i];
			int b = nums[j];
			int sum = abs(a + b);

			if (sum < min && a != b)
			{
				min = sum;
				ans[0] = a;
				ans[1] = b;
			}
		}
	}

	if (!ans.empty())
	{
		cout << ans[0] << " " << ans[1] << " " << min << endl;
	}
}

int main()
{
	string line;
	getline(cin, line);

	solveMethod(line);

	return 0;
}

🌷6.卡片组成的最大数字:

题目描述:

code:

// 卡片组成的最大数字
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

bool cmp(const string& s1, const string& s2)
{
	return s1 + s2 > s2 + s1;
}

int main()
{
	string line;
	getline(cin, line);

	// 将输入的数存储在数组nums中
	vector<string> nums;
	string card;
	for (int i = 0; i < line.size(); i++)
	{
		if (line[i] == ',')
		{
			nums.push_back(card);
			card.clear();
		}
		else
		{
			card += line[i];
		}
	}
	nums.push_back(card);

	sort(nums.begin(), nums.end(), cmp);

	string ans;
	for (auto& s : nums)
		ans += s;

	cout << ans << endl;

	return 0;
}

🌷7.找最小数:

题目描述:

code:

// 找最小数
#include <iostream>
#include <string>
#include <stack>

using namespace std;

int main()
{
	string str;
	cin >> str;

	int n;
	cin >> n;

	stack<char> st;
	for (int i = 0; i < str.size(); i++)
	{
		while (n > 0 && !st.empty() && st.top() > str[i])
		{
			st.pop();
			n--;
		}
		st.push(str[i]);
	}

	string ans;
	while (!st.empty())
	{
		ans = st.top() + ans;
		st.pop();
	}

	cout << ans << endl;
	return 0;
}

🌷8.身高排序:

题目描述:

code:

// 身高排序
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int tall, n;
	cin >> tall >> n;

	vector<pair<int, int>> talls(n);
	for (int i = 0; i < n; i++)
	{
		int t;
		cin >> t;
		talls[i] = { abs(tall - t),t };
	}

	sort(talls.begin(), talls.end());

	for (auto e : talls)
	{
		cout << e.second << " ";
	}
	return 0;
}

🌷9.出错的或电路:

题目描述:

code:

// 出错的或电路
#include <iostream>
#include <string>

using namespace std;

int count_the_number(const string& str, const char& ch)
{
	int count = 0;
	for (auto& e : str)
	{
		if (e == ch)
			count++;
	}
	return count;
}

int count_num(int n, const string& str1, const string& str2)
{
	int count = 0;
	for (int i = 0; i < n; i++)
	{
		if (str1[i] == '1' && str2[i] == '0')
			count++;
	}
	return count;
}

int main()
{
	int n;
	string str1, str2;
	cin >> n >> str1 >> str2;

	int count1 = count_the_number(str1, '1');
	int count2 = count_the_number(str2, '0');
	int count = count_num(n, str1, str2);

	int ans = (count1 - count) * (count2 - count) + (n - count1) * count;
	cout << ans << endl;

	return 0;
}

🌷10.磁盘容量:

题目描述:

code:

// 磁盘容量
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int convert(const string& str)
{
	int sum = 0;
	string s = str;
	int pos = 0;
	while ((pos = s.find_first_of("MGT")) != string::npos)
	{
		string  substr = s.substr(0, pos);
		int size = stoi(substr);
		switch (s[pos])
		{
		case 'M':
			sum += size;
			break;
		case 'G':
			sum += size * 1024;
			break;
		case 'T':
			sum += size * 1024 * 1024;
			break;
		default:
			break;
		}
		s = s.substr(pos + 1);
		pos = 0;
	}
	return sum;
}

bool compare(const string& str1, const string& str2)
{
	return convert(str1) < convert(str2);
}

int main()
{
	int n;
	cin >> n;

	vector<string> capacity(n);
	for (auto& e : capacity)
		cin >> e;

	sort(capacity.begin(), capacity.end(), compare);

	for (const auto& e : capacity)
		cout << e << endl;

	return 0;
}

坚持打卡!😃


http://www.kler.cn/news/107820.html

相关文章:

  • 已解决:conda找不到对应版本的cudnn如何解决?
  • K8s 部署 CNI 网络组件+k8s 多master集群部署+负载均衡
  • 猴子吃桃问题--C语言
  • 计算机操作系统重点概念整理-第五章 文件管理【期末复习|考研复习】
  • 不再受害:如何预防和应对.mallab勒索病毒攻击
  • Mac运行Docker报错
  • shell实现部署ftp提供共享yum仓库
  • QT中获取当前项目路径的写法
  • 已有wchar_t *类型的filepath,使用Qt打开该文件
  • CLIP文章精读
  • 【计算机毕设经典案例】基于微信小程序的图书管理系统
  • 水性杨花:揭秘CSS响应式界面设计,让内容灵活自如,犹如水之变幻
  • synchronized 的锁类型
  • 使用mac自带VNC公网远程控制macOS
  • [备忘.Linux]服务部署管理常用命令|systemd
  • JS条件表达式
  • [Python] OSError: [E050] Can‘t find model ‘en_core_web_sm‘.
  • 实用篇-认识微服务
  • stable-diffusion-ui 下载和安装
  • 【uniapp】仿微信支付界面
  • Day 46 动态规划 part12
  • 【问题】在安装torchvision的时候,会自动安装torch?
  • 【考研数学】数学“背诵”手册 | 需要记忆且容易遗忘的知识点
  • 软考系列(系统架构师)- 2009年系统架构师软考案例分析考点
  • 【JAVA学习笔记】50 - Math类,Array类,System类,BigInteger和BigDecimal类
  • IO流框架,缓冲流
  • 【Codeforces】 CF79D Password
  • CompletableFuture 实战
  • 公网远程访问macOS本地web服务器
  • 23种设计模式(10)——门面模式