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

目录

🌷1. VLAN 资源池:

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

🌷3. 数组合并:

🌷4. 分糖果:

🌷5. 数组组成的最小数字:

🌷6. 矩形相交的面积:

🌷7. 洞穴探险:

🌷8. 非严格递增连续数字序列:

🌷9. 找终点:

🌷10. 路灯照明:



🌷1. VLAN 资源池:

题目描述:

code: 

// 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: 

// 最少交换次数
#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: 

// 数组合并
#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: 

// 分糖果
#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: 

// 数组组成的最小数字
#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: 

// 矩形相交的面积
#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: 

// 洞穴探险
#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: 

// 非严格递增连续数字序列
#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: 

// 找终点
#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: 

// 路灯照明
#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;
}

坚持打卡!😃

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瞳绣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值