华为4.24机考

刚参加过4.24机考,记录一下。

机考源码,前两题案例全通过,但是我觉得第二题还有点问题,没有考虑完全。

第三题没做出来。

1、二叉搜索树的构建与查找

输入格式

第一行包含若干个用空格分隔的正整数,表示给定的数列。

第二行包含一个正整数,表示待查找的数。

输出格式

输出查找的路径和结果。

路径从根节点开始,用 S 表示。查找左子树用 L 表示,查找右子树用 R 表示。查找到结果用 Y 表示,未找到结果用 N 表示。

样例输入 1

2 1 3 7 5 6 4
6

样例输出 1

SRY

样例输入 2

4 2 1 3 6 5 7
5

样例输出 2

SRLY

样例输入 3

1 2 3 4 5 6 7
8

样例输出 3

SRRN

代码:

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
struct TreeNode {
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode(int val) : val(val), left(nullptr), right(nullptr) {}
};

static TreeNode* buildTree(vector<int>& nums, int start, int end) //中序数组建树
{
	if (start > end)
	{
		return nullptr;
	}
	int mid = (start + end) / 2;
	TreeNode* root = new TreeNode(nums[mid]);//根节点
	root->left = buildTree(nums, start, mid - 1);
	root->right = buildTree(nums, mid + 1, end);
	return root;
}

static string search(TreeNode* root, int target)
{
	stringstream path;
	string result;
	path << "S";
	TreeNode* cur = root;
	while (cur != nullptr)
	{
		if (cur->val == target)
		{
			path << "Y";
			break;
		}
		else if (target < cur->val)//小于就去
		{
			path << "L";
			cur = cur->left;
			if (cur == nullptr)
				break;
		}
		else
		{
			path << "R";
			cur = cur->right;
			if (cur == nullptr)
			{
				break;
			}
		}
	}
	result = path.str();
	if (path.str().back() != 'Y')
	{
		result[result.size() - 1] = 'N';
	}
	return result;
}

int main()
{
	string input;   //输入一串数字 空格分开
	getline(cin, input);
	stringstream ss(input);
	vector<int> nums;
	int num;
	while (ss >> num)
	{
		nums.push_back(num); //将树全部放入vector
	}
	sort(nums.begin(),nums.end());  //默认是升序
	TreeNode* root = buildTree(nums, 0, nums.size() - 1);
	int target;
	cin >> target;
	string result = search(root,target);
	cout << result << endl;
	return 0;
}	

二、球员能力评估

K教练正在对足球队的n名球员进行射门能力评估。评估共进行 m次训练,每次训练时,若球员射门得分则记为1,否则记为0。现在K教练需要根据以下规则对球员进行排名:

  1. 进球总数较多的球员排名靠前。

  2. 如果进球总数相同,则最长连续进球次数较多的球员排名靠前。

  3. 如果最长连续进球次数也相同,则第一次未进球的训练序号较大的球员排名靠前。如果第一次未进球的训练序号也相同,则比较第二次、第三次……直到比较出结果。

  4. 如果按照前三条规则仍然无法区分排名,则编号较小的球员排名靠前。

请你帮助K教练生成一个球员排名。

输入格式

第一行包含两个正整数 n 和m ,表示参与评估的球员数量和训练次数,球员编号从1到n 。

第二行包含n个空格分隔的长度为m的字符串,第i个字符串表示编号为i的球员在这m次训练中的进球记录。

输出格式

输出一行,包含n个空格分隔的正整数,表示球员编号按照射门能力从高到低排列的结果。

数据范围:1<= n <= 1000    1<=m <=1000

样例输入

4 5
11100 00111 10111 01111

样例输出

4 3 1 2

代码:

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>


class player {
public:
	int id;
	int totalGoals;
	int maxConsecutiveGoals;
	vector<int> missedOrdered;

};


class mycompare {
public:
	bool operator () (player& a, player& b)
	{
		if (a.totalGoals != b.totalGoals)
			return a.totalGoals > b.totalGoals;
		else if (a.maxConsecutiveGoals != b.maxConsecutiveGoals)
			return a.maxConsecutiveGoals > b.maxConsecutiveGoals;
		else
			return a.missedOrdered < b.missedOrdered;
	}
};

int main()
{
	int  n, m;
	cin >> n >> m;
	vector<player> players(n);
	int lianjicishu = 0;
	int maxlianji = 0;
	for (int i = 0; i < n; i++)
	{
		players[i].id = i + 1;
		players[i].totalGoals = 0;
		players[i].maxConsecutiveGoals = 0;
		players[i].missedOrdered.resize(m);

		string goal;
		cin >> goal;
		for (int j = 0; j < m; j++)//每轮比赛
		{
			players[i].totalGoals += (goal[j] - '0');
			if (goal[j] == '1')
			{
				lianjicishu++;
				maxlianji = lianjicishu;
			}
			else
				lianjicishu = 0;

			players[i].maxConsecutiveGoals = max(players[i].maxConsecutiveGoals, maxlianji);
			
			if (goal[j] == '0')
			{
				players[i].missedOrdered[j] = 1;
			}
		}
		lianjicishu = 0;
		maxlianji = 0;
	}

	sort(players.begin(),players.end(),mycompare());

	for (int i = 0; i < n; i++)
	{
		cout << players[i].id << " ";
	}
	return 0;
}

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值