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

目录

[🌷1. VLAN 资源池:](#🌷1. VLAN 资源池:)

[🌷2. 根据某条件聚类最少交换次数:](#🌷2. 根据某条件聚类最少交换次数:)

[🌷3. 数组合并:](#🌷3. 数组合并:)

[🌷4. 分糖果:](#🌷4. 分糖果:)

[🌷5. 数组组成的最小数字:](#🌷5. 数组组成的最小数字:)

[🌷6. 矩形相交的面积:](#🌷6. 矩形相交的面积:)

[🌷7. 洞穴探险:](#🌷7. 洞穴探险:)

[🌷8. 非严格递增连续数字序列:](#🌷8. 非严格递增连续数字序列:)

[🌷9. 找终点:](#🌷9. 找终点:)

[🌷10. 路灯照明:](#🌷10. 路灯照明:)


🌷1. VLAN 资源池:

题目描述:

code:

cpp 复制代码
// VLAN资源池
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> exchange(const string& str)
{
    // 将字符串转化为:单个数字/数字-数字的形式
    string s = str;
    vector<string> v;
    int pos = 0;
    while ((pos = s.find(',')) != string::npos)
    {
        v.push_back(s.substr(0, pos));
        s = s.substr(pos + 1);
    }
    v.push_back(s);

    // 将数组v转化为:单个数字进行存储
    vector<int> ans;
    for (int i = 0; i < v.size(); i++)
    {
        int pos = 0;
        if ((pos = v[i].find('-')) != string::npos)
        {
            int a = stoi(v[i].substr(0, pos));
            int b = stoi(v[i].substr(pos + 1));
            for (int i = a; i <= b; i++)
            {
                ans.push_back(i);
            }
        }
        else
        {
            ans.push_back(stoi(v[i]));
        }
    }

    return ans;
}

void eraseId(vector<int>& ans, int n)
{
    sort(ans.begin(), ans.end());

    // 找出在数组ans中:第一个大于等于n的位置
    auto it = lower_bound(ans.begin(), ans.end(), n);
    if (it != ans.end() && *it == n)
        ans.erase(it);
}

string exchangeToString(const vector<int>& ans)
{
    string s = to_string(ans[0]);
    for (int i = 1; i < ans.size(); i++)
    {
        if (i < ans.size() && ans[i - 1] + 1 == ans[i])
        {
            s += '-';
            while (i < ans.size() && ans[i - 1] + 1 == ans[i])
            {
                i++;
            }
            s += to_string(ans[i - 1]);
        }
        if (i < ans.size())
        {
            s += ',';
            s += to_string(ans[i]);
        }
    }
    return s;
}

int main()
{
    // 用于保存输入的数据
    string str;
    int n;
    cin >> str >> n;

    // 将输入的数据转换为数字形式进行存储
    vector<int> ans = exchange(str);

     eraseId(ans, n);

    string s = exchangeToString(ans);

    cout << s << endl;

    return 0;
}
🌷2. 根据某条件聚类最少交换次数:

题目描述:

code:

cpp 复制代码
// 最少交换次数
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>

using namespace std;

int main()
{
	// 用于读取输入的数据
	string line;
	int n;
	getline(cin, line);
	cin >> n;

	// 将读取的数据存放至数组:nums中
	vector<int> nums;
	int num;
	stringstream ss(line);
	while (ss >> num)
	{
		nums.push_back(num);
	}

	// 用flag数组存放:将小于n的位置置为1,大于n的位置置为0
	vector<int> flag;
	for (const auto& e : nums)
	{
		if (e < n)
			flag.push_back(1);
		else
			flag.push_back(0);
	}

	// 将小于n的1的位置相加
	int m = 0;
	for (const auto& e : flag)
		m += e;

	vector<int> dp(nums.size());
	for (int i = 0; i < nums.size(); i++)
	{
		for (int j = i; j < nums.size() && j < i + m; j++)
		{
			dp[i] += flag[j];
		}
	}

	int result = m - *max_element(dp.begin(), dp.end());

	cout << result << endl;

	return 0;
}
🌷3. 数组合并:

题目描述:

code:

cpp 复制代码
// 数组合并
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <sstream>

using namespace std;

string solveMethod(int k, int n, const vector<string>& lines)
{
	// 用于保存单个数据
	vector<list<int>> lists;
	for (int i = 0; i < n; i++)
	{
		list<int> list;
		stringstream ss(lines[i]);
		string s;
		while (getline(ss, s, ','))
		{
			list.push_back(stoi(s));
		}
		lists.push_back(list);
	}

	// 将数组进行合并
	int index = 0;
	string ret;
	while (lists.size() > 0)
	{
		for (int i = 0; i < k; i++)
		{
			if (lists[index].empty())
			{
				lists.erase(lists.begin() + index);
				index--;
				break;
			}
			ret += to_string(lists[index].front());
			ret += ',';
			lists[index].pop_front();
		}
		index ++;
		if (index >= lists.size())
			index = 0;
	}
	return ret;
}

int main()
{
	// 用于保存输入的数据
	int k, n;
	cin >> k >> n;
	vector<string> lines(n);
	for (int i = 0; i < n; i++)
	{
		cin >> lines[i];
	}

	// 用于保存输出的数据
	string result = solveMethod(k, n, lines);

	cout << result << endl;

	return 0;
}
🌷4. 分糖果:

题目描述:

code:

cpp 复制代码
// 分糖果
#include <iostream>

using namespace std;

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

	int count = 0;
	for (int i = n; i != 1; i /= 2, count++)
	{
		if (i == 3)
		{
			count += 2;
			break;
		}
		if (i % 2 != 0)
		{
			if ((i + 1) / 2 % 2 == 0)
				i++;
			else
				i--;
			count++;
		}
	}

	cout << count << endl;

	return 0;
}
🌷5. 数组组成的最小数字:

题目描述:

code:

cpp 复制代码
// 数组组成的最小数字
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>

using namespace std;

int main()
{
	// 用于保存读入的数据
	string read;
	cin >> read;

	// 用于将读入的数据保存在数组中
	vector<int> save;
	stringstream ss(read);
	string single;
	while (getline(ss, single, ','))
	{
		save.push_back(stoi(single));
	}

	string output;
	int len = save.size();
	if (len == 1)
		output += save[0];
	else
	{
		sort(save.begin(), save.end());
		vector<string> media;
		int realLen = (len == 2) ? 2 : 3;
		for (int i = 0; i < realLen; i++)
		{
			media.push_back(to_string(save[i]));
		}
		sort(media.begin(), media.end());
		for (const auto& s : media)
		{
			output += s;
		}
	}

	cout << output << endl;

	return 0;
}
🌷6. 矩形相交的面积:

题目描述:

code:

cpp 复制代码
// 矩形相交的面积
#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>

using namespace std;

struct Point
{
	int x1;
	int y1;
	int x2;
	int y2;

	Point(int x1, int y1, int x2, int y2)
		:x1(x1), y1(y1), x2(x2), y2(y2)
	{}
};

int min(const vector<int>& v)
{
	int min = INT_MAX;
	for (const auto& e : v)
	{
		if (e < min)
			min = e;
	}
	return min;
}

int max(const vector<int>& v)
{
	int max = INT_MIN;
	for (const auto& e : v)
	{
		if (e > max)
			max = e;
	}
	return max;
}

int main()
{
	vector<Point> points;
	for (int i = 0; i < 3; i++)
	{
		int x1, y1, w, h;
		cin >> x1 >> y1 >> w >> h;
		int x2 = x1 + w;
		int y2 = y1 - h;
		points.push_back(Point(x1, y1, x2, y2));
	}

	vector<int> xs, ys;
	for (const auto& e : points)
	{
		xs.push_back(e.x1);
		xs.push_back(e.x2);
		ys.push_back(e.y1);
		ys.push_back(e.y2);
	}

	int min_x = min(xs);
	int max_x = max(xs);
	int min_y = min(ys);
	int max_y = max(ys);

	vector<vector<int>> dp(max_x - min_x + 1, vector<int>(max_y - min_y + 1, 0));

	for (const auto& p : points)
	{
		int x1 = p.x1 - min_x;
		int x2 = p.x2 - min_x;
		int y1 = p.y1 - min_y;
		int y2 = p.y2 - min_y;
		for (int i = min(x1, x2); i < max(x1, x2); i++)
		{
			for (int j = min(y1, y2); j < max(y1, y2); j++)
			{
				dp[i][j]++;
			}
		}
	}

	int ret = 0;
	for (const auto& row : dp)
	{
		for (const auto& e : row)
		{
			if (e == 3)
				ret++;
		}
	}

	cout << ret << endl;

	return 0;
}
🌷7. 洞穴探险:

题目描述:

code:

cpp 复制代码
// 洞穴探险
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

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

	int index = 0, len = 0, max = 0, x = 0, y = 0, l = 0, r = 0;
	while (true)
	{
		str = str.substr(index);
		l = str.find('(');
		r = str.find(')');

		if (l == -1)
			break;

		string substr = str.substr(l + 1, r - l - 1);
		stringstream ss(substr);
		vector<string> nums;
		string s;
		while (getline(ss, s, ','))
		{
			nums.push_back(s);
		}

		if (nums[0].find('0') != 0 && nums[1].find('0') != 0)
		{
			int a = stoi(nums[0]);
			int b = stoi(nums[1]);
			len = a * a + b * b;
			if (len > max)
			{
				max = len;
				x = a;
				y = b;
			}
		}
		index = r + 1;
	}

	cout << '(' << x << ',' << y << ')' << endl;

	return 0;
}
🌷8. 非严格递增连续数字序列:

题目描述:

code:

cpp 复制代码
// 非严格递增连续数字序列
#include <iostream>
#include <string>

using namespace std;

int solveMethod(const string& str)
{
	int curLen = 0, maxLen = 0;

	// 只是用于记录当前字符的上一字符,没有特定的赋值
	char past = 'a';

	for (const char cur : str)
	{
		if (cur >= '0' && cur <= '9')
		{
			if (curLen == 0 || cur >= past)
				curLen++;
			else
			{
				if (curLen > maxLen)
				{
					maxLen = curLen;
				}
				curLen = 1;
			}
			past = cur;
		}
		else
		{
			if (curLen > maxLen)
			{
				maxLen = curLen;
			}
			curLen = 0;
			past = 'a';
		}
	}

	maxLen = max(curLen, maxLen);

	return maxLen;
}

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

	int count = solveMethod(str);

	cout << count << endl;

	return 0;
}
🌷9. 找终点:

题目描述:

code:

cpp 复制代码
// 找终点
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <limits.h>

using namespace std;

vector<int> exchange(const string& str)
{
	stringstream ss(str);
	string s;
	vector<int> save;
	while (getline(ss, s, ' '))
	{
		save.push_back(stoi(s));
	}
	return save;
}

int minStep(const vector<int>& save)
{
	int min_i = 1;
	int max_i = save.size() / 2;
	int current = 0;
	int target = save.size() - 1;
	int minVal = INT_MAX;
	int count = 0;

	for (int i = min_i; i < max_i; i++)
	{
		current = i;
		count++;

		while (current < target)
		{
			current += save[current];
			count++;
		}

		if (current == target)
		{
			minVal = min(minVal, count);
			count = 0;
			continue;
		}

		if (current > target)
		{
			count = 0;
			continue;
		}
	}

	if (minVal <= save.size())
		return minVal;
	else
		return -1;
}

int main()
{
	// 用于保存读入的数据
	string read;
	getline(cin, read);

	// 将读入的数据以数组的形式存储
	vector<int> save = exchange(read);

	// 输出最小的步数
	int count = minStep(save);

	cout << count << endl;

	return 0;
}
🌷10. 路灯照明:

题目描述:

code:

cpp 复制代码
// 路灯照明
#include <iostream>
#include <vector>

using namespace std;

void solveMethod(const vector<int>& save)
{
	vector<bool> bytes((save.size() - 1) * 100, 0);

	for (int i = 0; i < save.size(); i++)
	{
		int left = max(0, i * 100 - save[i]);
		int right = min((int)bytes.size(), i * 100 + save[i]);
		for (int k = left; k < right; k++)
		{
			bytes[k] = 1;
		}
	}

	int count = 0;
	for (const auto& e : bytes)
	{
		if (e == 0)
			count++;
	}
	cout << count << endl;
}

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

	vector<int> save(n);
	for (auto& e : save)
		cin >> e;

	solveMethod(save);

	return 0;
}

坚持打卡!😃

相关推荐
luky!18 分钟前
算法--解决熄灯问题
python·算法
Xiao Fei Xiangζั͡ޓއއ19 分钟前
一觉睡醒,全世界计算机水平下降100倍,而我却精通C语言——scanf函数
c语言·开发语言·笔记·程序人生·面试·蓝桥杯·学习方法
记录无知岁月22 分钟前
【MATLAB】目标检测初探
开发语言·yolo·目标检测·matlab·yolov3·yolov2
鸽鸽程序猿24 分钟前
【算法】【优选算法】二分查找算法(下)
java·算法·二分查找算法
_OLi_26 分钟前
力扣 LeetCode 150. 逆波兰表达式求值(Day5:栈与队列)
算法·leetcode·职场和发展
远望清一色36 分钟前
基于MATLAB身份证号码识别
开发语言·图像处理·算法·matlab
NMBG2243 分钟前
[JAVAEE] 面试题(四) - 多线程下使用ArrayList涉及到的线程安全问题及解决
java·开发语言·面试·java-ee·intellij-idea
Py小趴1 小时前
Python自学之Colormaps指南
开发语言·python·数据可视化
晒足以百八十1 小时前
基于Python 和 pyecharts 制作招聘数据可视化分析大屏
开发语言·python·信息可视化
敲代码不忘补水1 小时前
生成式GPT商品推荐:精准满足用户需求
开发语言·python·gpt·产品运营·产品经理