刚参加过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教练需要根据以下规则对球员进行排名:
-
进球总数较多的球员排名靠前。
-
如果进球总数相同,则最长连续进球次数较多的球员排名靠前。
-
如果最长连续进球次数也相同,则第一次未进球的训练序号较大的球员排名靠前。如果第一次未进球的训练序号也相同,则比较第二次、第三次……直到比较出结果。
-
如果按照前三条规则仍然无法区分排名,则编号较小的球员排名靠前。
请你帮助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;
}