1. N = 3k
1,2,4,5..............3k-2 3k-1 剩下 2 * N / 3个。r = f(2k)
则f(N) = ((r-1)/2) * 3 + 2 - r%2
2. N = 3k + 1, r = f(2k+1)
则 if r = 1
f(N) = 3k+1;
else
f(N) = ((r-2)/2) * 3 + 2 - (r-1)%2
3. N = 3k+2,r = f(2k+2)
if r = 1
f(N) = 3k+1
else if r = 2
f(N) = 3k+2
else
f(N) = ((r-3)/2) * 3 +2 - (r-2)%2
1
2 #include <iostream>
3 using namespace std;
4
5 int f(int n)
6 {
7 if(n == 2)
8 return 2;
9 int k = n / 3;
10 if(n % 3 == 0)
11 {
12 int r = f(2*k);
13 return ((r - 1)/2) * 3 + 2 - r%2;
14 }else if(n%3 == 1)
15 {
16 int r = f(2*k+1);
17 if (r == 1)
18 return 3*k+1;
19 return ((r - 2)/2) * 3 + 2 - (r-1)%2;
20 }
21 else{
22 int r = f(2*k+2);
23 if (r == 1)
24 {
25 return 3*k+1;
26 }
27 if (r == 2)
28 {
29 return 3*k+2;
30 }
31 return ((r - 3)/2) * 3 + 2 - (r-2)%2;
32 }
33 }
34 int main()
35 {
36 cout << f(17) << endl;
37 return 0;
38 }
2 #include <iostream>
3 using namespace std;
4
5 int f(int n)
6 {
7 if(n == 2)
8 return 2;
9 int k = n / 3;
10 if(n % 3 == 0)
11 {
12 int r = f(2*k);
13 return ((r - 1)/2) * 3 + 2 - r%2;
14 }else if(n%3 == 1)
15 {
16 int r = f(2*k+1);
17 if (r == 1)
18 return 3*k+1;
19 return ((r - 2)/2) * 3 + 2 - (r-1)%2;
20 }
21 else{
22 int r = f(2*k+2);
23 if (r == 1)
24 {
25 return 3*k+1;
26 }
27 if (r == 2)
28 {
29 return 3*k+2;
30 }
31 return ((r - 3)/2) * 3 + 2 - (r-2)%2;
32 }
33 }
34 int main()
35 {
36 cout << f(17) << endl;
37 return 0;
38 }