• 笔试面试相关记录(6)


    (1)不包含重复字符的最长子串的长度

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int getMaxLength(string& s) {
    6. int len = s.size();
    7. map<char, int> mp;
    8. int max_len = 0;
    9. int left = 0;
    10. int i = 0;
    11. while (i < len) {
    12. if (mp[s[i]] > 0) {
    13. max_len = max(max_len, i-left);
    14. while (mp[s[i]] > 0) {
    15. mp[s[left]]--;
    16. left++;
    17. }
    18. }
    19. mp[s[i]]++;
    20. i++;
    21. }
    22. max_len = max(max_len, i-left);
    23. return max_len;
    24. }
    25. int main() {
    26. string str;
    27. while(cin >> str) {
    28. cout << getMaxLength(str) << endl;
    29. }
    30. return 0;
    31. }

    (2)小米基站问题,基站数组中,每个基站的x,y,q分别表示基站的坐标x和y,以及信号q,radius表示手机可以接收信号的范围,当手机距离基站的距离小于radius时,基站的信号为floor(q/(1+d)),求出一个信号最强的位置,如果有多个位置,按照字典序取第一个。

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. double distance(int x1, int y1, int x2, int y2) {
    7. return sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
    8. }
    9. int main() {
    10. string str;
    11. std::getline(std::cin, str);
    12. int t = 0, n, radius;
    13. for (long unsigned i = 0; i < str.size(); i++) {
    14. if (isdigit(str[i])) {
    15. t = t*10 + (str[i]-'0');
    16. } else {
    17. n = t;
    18. t = 0;
    19. }
    20. }
    21. radius = t;
    22. int x, y, q;
    23. int min_x = 1e8, min_y = 1e8, max_x = 0, max_y = 0;
    24. vectorint>> vec;
    25. for (int i = 0; i < n; i++) {
    26. scanf("%d,%d,%d", &x, &y, &q);
    27. vec.push_back({x,y,q});
    28. min_x = min(x, min_x);
    29. min_y = min(y, min_y);
    30. max_x = max(x, max_x);
    31. max_y = max(y, max_y);
    32. }
    33. vectorint>> grid(max_x+1, vector<int>(max_y+1, 0));
    34. int max_q = 0, ans_x = 1e8, ans_y = 1e8;
    35. for (int i = 0; i < n; i++) {
    36. int n_x = vec[i][0], n_y = vec[i][1], q = vec[i][2];
    37. for (int j = -radius; j <= radius; j++) {
    38. for (int k = -radius; k <= radius; k++) {
    39. int new_x = n_x + j, new_y = n_y + k;
    40. double dis = distance(new_x, new_y, n_x, n_y);
    41. if (new_x >= 0 && new_y >= 0 && new_x <= max_x && new_y <= max_y && dis < radius) {
    42. grid[new_x][new_y] += floor(q/(1+dis));
    43. if (grid[new_x][new_y] > max_q) {
    44. max_q = grid[new_x][new_y];
    45. ans_x = new_x;
    46. ans_y = new_y;
    47. } else if (grid[new_x][new_y] == max_q && (new_x < ans_x || (new_x == ans_x && new_y < ans_y))) {
    48. ans_x = new_x;
    49. ans_y = new_y;
    50. }
    51. }
    52. }
    53. }
    54. }
    55. cout << ans_x << "," << ans_y << endl;
    56. }

    (3)是一个拓扑排序问题,

    输入n,表示n个任务

    输入1:0,0:1表示任务1依赖任务0,任务0依赖任务1,问这个任务能不能完成?可以完成输出1,否则输出0;

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. int main() {
    7. int n;
    8. string str;
    9. while (cin >> n) {
    10. cin >> str;
    11. int t = 0;
    12. int a = 0, b = 0;
    13. int max_a = 0, max_b = 0;
    14. vectorint>> vec;
    15. for (long unsigned i = 0; i < str.size(); i++) {
    16. if (isdigit(str[i])) {
    17. t = t*10 + (str[i]-'0');
    18. } else if (str[i] == ':') {
    19. a = t;
    20. t = 0;
    21. } else if (str[i] == ',') {
    22. max_a = max(max_a, a);
    23. max_b = max(max_b, t);
    24. vec.push_back({a, t});
    25. t = 0;
    26. }
    27. }
    28. max_a = max(max_a, a);
    29. max_b = max(max_b, t);
    30. vec.push_back({a, t});
    31. vectorint>> grid(max_a+1, vector<int>(max_b+1, 0));
    32. for (int i = 0; i < n; i++) {
    33. grid[vec[i][0]][vec[i][1]] = 1;
    34. }
    35. queue<int> q;
    36. vector<int> dege(n, 0);
    37. for (int i = 0; i < n; i++) {
    38. int count = 0;
    39. for (int j = 0; j < n; j++) {
    40. if (grid[i][j] = 1) {
    41. count++;
    42. }
    43. }
    44. dege[i] = count;
    45. if (count == 0) {
    46. q.push(i);
    47. }
    48. }
    49. while (q.size()) {
    50. int node = q.front();
    51. q.pop();
    52. for (int i = 0; i < n; i++) {
    53. if (grid[i][node] == 1) {
    54. grid[i][node] = 0;
    55. dege[i]--;
    56. if (dege[i] == 0) {
    57. q.push(i);
    58. }
    59. }
    60. }
    61. }
    62. bool flag = false;
    63. for (int i = 0; i < n; i++) {
    64. if (dege[i]) {
    65. flag = true;
    66. break;
    67. }
    68. }
    69. if (flag) {
    70. cout << 0 << endl;
    71. } else {
    72. cout << 1 << endl;
    73. }
    74. }
    75. return 0;
    76. }

    (4)一个数组,表示每个工人的工作能力,现有一个工作需要ceil(n/2.0)个工人去完成,并且要求这些工人的工作能力之和大于等于target,求有多少种安排工人的方法。

    输入

    1(测试组数)

    5 10(n,target)

    3 2 3 4 5

    输出7

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. using namespace std;
    7. void backtrace(vector<int>& nums, int sum, long unsigned path, int& ans, long unsigned start, long unsigned worker_num, int target, string str, set& set_str) {
    8. if (path == worker_num && sum >= target && set_str.find(str) == set_str.end()) {
    9. ans++;
    10. set_str.insert(str);
    11. return;
    12. }
    13. if (start >= nums.size() || path > worker_num) return;
    14. int len = nums.size();
    15. // 这个语句放在for循环外面,用于记录上次的结果,不放入for循环中
    16. string t = str;
    17. for (long unsigned i = start; i < len; i++) {
    18. // 这里一个错误导致弄了半天也没有弄出来,
    19. if (path == 0) str = to_string(i);
    20. // 下面的代码之前写的是str = str + "-" + to_string(i);这样写是有错误的
    21. // 因为每次选或者不选,是从上一次的结果后面进行添加的,如果这里使用了str = str + "-" + to_string(i);
    22. // 就会导致str在for循环中会被修改。
    23. else str = t + "-" + to_string(i);
    24. // 如果当前位置选择,则在上一次的结果上加上这次的数字
    25. backtrace(nums, sum+nums[i], path+1, ans, i+1, worker_num, target, str, set_str);
    26. // 当前位置不选择
    27. backtrace(nums, sum, path, ans, i+1, worker_num, target, t, set_str);
    28. }
    29. }
    30. int main() {
    31. int T;
    32. while (cin >> T) {
    33. for (int times = 0; times < T; times++) {
    34. int n, target;
    35. cin >> n >> target;
    36. vector<int> nums(n, 0);
    37. int t;
    38. for (int i = 0; i < n; i++) {
    39. cin >> t;
    40. nums[i] = t;
    41. }
    42. long unsigned path = 0;
    43. // 如果使用ceil函数,记得要将其变成float或者double形式,如果是整数,就会出错。
    44. long unsigned worker_num = (n%2==1)?(n+1)/2:(n/2);
    45. string str = "";
    46. int ans = 0;
    47. set set_str;
    48. backtrace(nums, 0, path, ans, 0, worker_num, target, str, set_str);
    49. cout << ans << endl;
    50. }
    51. }
    52. return 0;
    53. }

    (5)

    1. #include
    2. using namespace std;
    3. void func(int a, int b, int c) {
    4. c = a*b;
    5. }
    6. int main()
    7. {
    8. int c;
    9. int a = 3, b = 3;
    10. func(a, b, c);
    11. cout << c << endl;
    12. printf("%d", c);
    13. return 0;
    14. }
    15. 输出随机数,不是输出0

    (6)

    1. #include
    2. using namespace std;
    3. class T {
    4. public:
    5. T() {cout << "T()" << endl;}
    6. };
    7. int main()
    8. {
    9. T* p = new T[3];
    10. return 0;
    11. }
    12. 输出
    13. T()
    14. T()
    15. T()

  • 相关阅读:
    LeetCode_2598_剑指Offer Ⅱ 091.粉刷房子
    三个神仙电脑端的工具,快进来看看你知不知道这些工具
    IOS – OpenGL ES 图像侵蚀边缘色彩模糊 GPUImageRGBErosionFilter
    Python 数据清洗:pd.cut()分箱统计
    建立时间和保持时间的模型分析
    试题:最大的矩形(给定直方图里面积最大的矩形)
    两个移相算法
    国庆加速度!新增功能点锁定功能,敏捷开发新增估算功能,助力项目快速突破!
    Docker使用总结
    鸿蒙开发之ArkTS 基础九 枚举类型
  • 原文地址:https://blog.csdn.net/wj617906617/article/details/133098770