2014 School Recruit Test Questions (C + + development Class)

Source: Internet
Author: User



First question :



1, 2, 3 .... n Lamps, while there are n individuals,



The 1th man pulls the light of a multiple of 1,



The 2nd man pulls the light of a multiple of 2,



......



The last few lights were lit,



The light is off in the initial state,



Enter an integer n (n<65536),



Number of lights in the output light






Method parsing: Set a map, the keyword is the number of the lamp, the value is 0 or 1, respectively, indicating the lights off and light state, traverse map, the first I light, all I multiples of the lamp to take the reverse operation, can also be achieved through an array. The code is as follows:





[CPP]View Plaincopy
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <map>
  5. Using namespace std;
  6. int count1 (int n)
  7. {
  8. vector<int> Vec (65536, 0);
  9. For (int i = 1; I <= n; i++)
  10. {
  11. For (int j = 1; J <= n;j++)
  12. {
  13. if (j%i== 0)
  14. VEC[J-1] =!vec[j-1];;
  15. }
  16. }
  17. int sum = 0;
  18. For (int j = 0; J < N; j + +)
  19. {
  20. if (vec[j]! = 0) //Statistics The number of light bulbs on
  21. sum++;
  22. }
  23. return sum;
  24. }
  25. int count2 (int n)
  26. {
  27. map<int, int> m;
  28. For (int i = 1; I <= n; i++) //Initialize map
  29. M.insert ({i, 0});
  30. For (int j = 1; J <= N; j + +)
  31. {
  32. For (Auto it = M.begin (); It! = M.end (); it++)
  33. {
  34. if (it->first%j = = 0)
  35. It->second =! (It->second);
  36. }
  37. }
  38. int sum = 0;
  39. For (Auto it = M.begin (); It! = M.end (); it++)
  40. if (It->second = = 1)
  41. sum++;
  42. return sum;
  43. }
  44. int count3 (int n)
  45. {
  46. int sum = 0;
  47. bool a[65536] = {0};
  48. For (int i = 1; I <= n; i++) //through two nested loops simulating n personal one-time pull -out light
  49. {
  50. For (int j = 1; J <= N; j + +)
  51. {
  52. if (j% i = = 0)
  53. {
  54. A[J-1] =!a[j-1];
  55. }
  56. }
  57. }
  58. For (int j = 0; J < N; j + +)
  59. {
  60. if (a[j]! = 0) //Statistics The number of light bulbs on
  61. sum++;
  62. }
  63. return sum;
  64. }
  65. int _tmain (int argc, _tchar* argv[])
  66. {
  67. int number1 = count1 (100);
  68. cout << number1 << Endl;
  69. return 0;
  70. }





The second question :



Enter an integer (n is an integer less than 9 digits), treated as a string to see if there is the same substring, such as 1212, the same substring is 12,141516 no substring (substring must be greater than or equal to 2), if there is the same substring output 1, otherwise output 0,



Input: Integers less than 9 in length



Output: 1 or 0






Method Analysis: The first consideration of the problem when the brain short-circuit, always the length of 2, 3, 4 of the substring of the case alone, and then only to think that the length of the 2 is the same as the substring. In addition, for the member function of the string type substr () parameter is somewhat forgotten, the function's two parameters indicate the starting position of the substring and the length of the substring respectively. The implementation code is as follows:





[CPP]View Plaincopy
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <iostream>
  4. Using namespace std;
  5. int fun1 (string &s) //Method One: Use a character array to solve
  6. {
  7. int i = 0, J;
  8. int res = 0;
  9. For (i = 2; I < S.size ()-1; i++)
  10. {
  11. For (j = i-1; J >= 0; j--)
  12. {
  13. if (s[i] = = S[j] && s[i + 1] = = s[j + 1])
  14. {
  15. res = 1;
  16. Break ;
  17. }
  18. }
  19. if (res = = 1)
  20. Break ;
  21. }
  22. return res;
  23. }
  24. int fun2 (string &s) //Method Two: Use string-type substrings to determine
  25. {
  26. int flag = 0;
  27. For (int i = 0; i < s.size ()-1; i++)
  28. {
  29. For (int J = i + 2; j < S.size ()-1; j + +)
  30. {
  31. if (s.substr (i,2) = = S.substr (j,2))
  32. {
  33. flag = 1;
  34. Break ;
  35. }
  36. if (flag)
  37. Break ;
  38. }
  39. }
  40. return flag;
  41. }
  42. int _tmain (int argc, _tchar* argv[])
  43. {
  44. string s = "13214121";
  45. cout <<fun1 (s) << Endl;
  46. Cout<<fun2 (s) <<endl;
  47. return 0;
  48. }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.