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
-
- #include "stdafx.h"
-
- #include <iostream>
-
- #include <vector>
-
- #include <map>
-
- Using namespace std;
-
- int count1 (int n)
-
- {
-
- vector<int> Vec (65536, 0);
-
- For (int i = 1; I <= n; i++)
-
- {
-
- For (int j = 1; J <= n;j++)
-
- {
-
- if (j%i== 0)
-
- VEC[J-1] =!vec[j-1];;
-
- }
-
- }
-
- int sum = 0;
-
- For (int j = 0; J < N; j + +)
-
- {
-
- if (vec[j]! = 0) //Statistics The number of light bulbs on
-
- sum++;
-
- }
-
- return sum;
-
- }
-
-
-
- int count2 (int n)
-
- {
-
- map<int, int> m;
-
- For (int i = 1; I <= n; i++) //Initialize map
-
- M.insert ({i, 0});
-
- For (int j = 1; J <= N; j + +)
-
- {
-
- For (Auto it = M.begin (); It! = M.end (); it++)
-
- {
-
- if (it->first%j = = 0)
-
- It->second =! (It->second);
-
- }
-
- }
-
- int sum = 0;
-
- For (Auto it = M.begin (); It! = M.end (); it++)
-
- if (It->second = = 1)
-
- sum++;
-
- return sum;
-
- }
-
- int count3 (int n)
-
- {
-
- int sum = 0;
-
- bool a[65536] = {0};
-
- For (int i = 1; I <= n; i++) //through two nested loops simulating n personal one-time pull -out light
-
- {
-
- For (int j = 1; J <= N; j + +)
-
- {
-
- if (j% i = = 0)
-
- {
-
- A[J-1] =!a[j-1];
-
- }
-
- }
-
- }
-
- For (int j = 0; J < N; j + +)
-
- {
-
- if (a[j]! = 0) //Statistics The number of light bulbs on
-
- sum++;
-
- }
-
- return sum;
-
- }
-
- int _tmain (int argc, _tchar* argv[])
-
- {
-
- int number1 = count1 (100);
-
- cout << number1 << Endl;
-
- return 0;
-
- }
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
-
- #include "stdafx.h"
-
- #include <string>
-
- #include <iostream>
-
- Using namespace std;
-
- int fun1 (string &s) //Method One: Use a character array to solve
-
- {
-
- int i = 0, J;
-
- int res = 0;
-
- For (i = 2; I < S.size ()-1; i++)
-
- {
-
- For (j = i-1; J >= 0; j--)
-
- {
-
- if (s[i] = = S[j] && s[i + 1] = = s[j + 1])
-
- {
-
- res = 1;
-
- Break ;
-
- }
-
- }
-
- if (res = = 1)
-
- Break ;
-
- }
-
- return res;
-
- }
-
- int fun2 (string &s) //Method Two: Use string-type substrings to determine
-
- {
-
- int flag = 0;
-
- For (int i = 0; i < s.size ()-1; i++)
-
- {
-
- For (int J = i + 2; j < S.size ()-1; j + +)
-
- {
-
- if (s.substr (i,2) = = S.substr (j,2))
-
- {
-
- flag = 1;
-
- Break ;
-
- }
-
- if (flag)
-
- Break ;
-
- }
-
- }
-
- return flag;
-
- }
-
- int _tmain (int argc, _tchar* argv[])
-
- {
-
- string s = "13214121";
-
- cout <<fun1 (s) << Endl;
-
- Cout<<fun2 (s) <<endl;
-
- return 0;
-
- }
-
-