当前位置:   article > 正文

华为4.24机考_4.24华为机试

4.24华为机试

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

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

第三题没做出来。

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

输入格式

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

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

输出格式

输出查找的路径和结果。

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

样例输入 1

  1. 2 1 3 7 5 6 4
  2. 6

样例输出 1

SRY

样例输入 2

  1. 4 2 1 3 6 5 7
  2. 5

样例输出 2

SRLY

样例输入 3

  1. 1 2 3 4 5 6 7
  2. 8

样例输出 3

SRRN

代码:

  1. #include <iostream>
  2. using namespace std;
  3. #include <vector>
  4. #include <algorithm>
  5. #include <string>
  6. #include <sstream>
  7. struct TreeNode {
  8. int val;
  9. TreeNode* left;
  10. TreeNode* right;
  11. TreeNode(int val) : val(val), left(nullptr), right(nullptr) {}
  12. };
  13. static TreeNode* buildTree(vector<int>& nums, int start, int end) //中序数组建树
  14. {
  15. if (start > end)
  16. {
  17. return nullptr;
  18. }
  19. int mid = (start + end) / 2;
  20. TreeNode* root = new TreeNode(nums[mid]);//根节点
  21. root->left = buildTree(nums, start, mid - 1);
  22. root->right = buildTree(nums, mid + 1, end);
  23. return root;
  24. }
  25. static string search(TreeNode* root, int target)
  26. {
  27. stringstream path;
  28. string result;
  29. path << "S";
  30. TreeNode* cur = root;
  31. while (cur != nullptr)
  32. {
  33. if (cur->val == target)
  34. {
  35. path << "Y";
  36. break;
  37. }
  38. else if (target < cur->val)//小于就去
  39. {
  40. path << "L";
  41. cur = cur->left;
  42. if (cur == nullptr)
  43. break;
  44. }
  45. else
  46. {
  47. path << "R";
  48. cur = cur->right;
  49. if (cur == nullptr)
  50. {
  51. break;
  52. }
  53. }
  54. }
  55. result = path.str();
  56. if (path.str().back() != 'Y')
  57. {
  58. result[result.size() - 1] = 'N';
  59. }
  60. return result;
  61. }
  62. int main()
  63. {
  64. string input; //输入一串数字 空格分开
  65. getline(cin, input);
  66. stringstream ss(input);
  67. vector<int> nums;
  68. int num;
  69. while (ss >> num)
  70. {
  71. nums.push_back(num); //将树全部放入vector
  72. }
  73. sort(nums.begin(),nums.end()); //默认是升序
  74. TreeNode* root = buildTree(nums, 0, nums.size() - 1);
  75. int target;
  76. cin >> target;
  77. string result = search(root,target);
  78. cout << result << endl;
  79. return 0;
  80. }

二、球员能力评估

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

样例输入

  1. 4 5
  2. 11100 00111 10111 01111

样例输出

4 3 1 2

代码:

  1. #include <iostream>
  2. using namespace std;
  3. #include <vector>
  4. #include <algorithm>
  5. #include <string>
  6. class player {
  7. public:
  8. int id;
  9. int totalGoals;
  10. int maxConsecutiveGoals;
  11. vector<int> missedOrdered;
  12. };
  13. class mycompare {
  14. public:
  15. bool operator () (player& a, player& b)
  16. {
  17. if (a.totalGoals != b.totalGoals)
  18. return a.totalGoals > b.totalGoals;
  19. else if (a.maxConsecutiveGoals != b.maxConsecutiveGoals)
  20. return a.maxConsecutiveGoals > b.maxConsecutiveGoals;
  21. else
  22. return a.missedOrdered < b.missedOrdered;
  23. }
  24. };
  25. int main()
  26. {
  27. int n, m;
  28. cin >> n >> m;
  29. vector<player> players(n);
  30. int lianjicishu = 0;
  31. int maxlianji = 0;
  32. for (int i = 0; i < n; i++)
  33. {
  34. players[i].id = i + 1;
  35. players[i].totalGoals = 0;
  36. players[i].maxConsecutiveGoals = 0;
  37. players[i].missedOrdered.resize(m);
  38. string goal;
  39. cin >> goal;
  40. for (int j = 0; j < m; j++)//每轮比赛
  41. {
  42. players[i].totalGoals += (goal[j] - '0');
  43. if (goal[j] == '1')
  44. {
  45. lianjicishu++;
  46. maxlianji = lianjicishu;
  47. }
  48. else
  49. lianjicishu = 0;
  50. players[i].maxConsecutiveGoals = max(players[i].maxConsecutiveGoals, maxlianji);
  51. if (goal[j] == '0')
  52. {
  53. players[i].missedOrdered[j] = 1;
  54. }
  55. }
  56. lianjicishu = 0;
  57. maxlianji = 0;
  58. }
  59. sort(players.begin(),players.end(),mycompare());
  60. for (int i = 0; i < n; i++)
  61. {
  62. cout << players[i].id << " ";
  63. }
  64. return 0;
  65. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号