随手练——HDU-1210 洗牌问题（模拟）

```#include <iostream>
using namespace std;

int main() {
int N;
cin >> N;
int *a = new int[2 * N + 1];
int *b = new int[2 * N + 1];
for (int i = 0; i <= 2 * N; i++) {
a[i] = i;
}

for (int i = 1; i <= 2 * N; i++) {
b[(i * 2) % (2 * N + 1)] = a[i];
}
for (int i = 1; i <= 2 * N; i++) {
cout << b[i] << " ";
}
cout << endl;

for (int i = 1; i <= 2 * N; i++) {
a[(i * 2) % (2 * N + 1)] = b[i];
}
for (int i = 1; i <= 2 * N; i++) {
cout << a[i] << " ";
}
cout << endl;
for (int k = 0; a[1]!=1&&b[1]!=1; k++) {
if (!(k & 1)) {
for (int i = 1; i <= 2 * N; i++) {
b[(i * 2) % (2 * N + 1)] = a[i];
}
for (int i = 1; i <= 2 * N; i++) {
cout << b[i] << " ";
}
cout << endl;
}
else {
for (int i = 1; i <= 2 * N; i++) {
a[(i * 2) % (2 * N + 1)] = b[i];
}
for (int i = 1; i <= 2 * N; i++) {
cout << a[i] << " ";
}
cout << endl;
}

}

return 0;
}```
View Code

```#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
int n;
while (~scanf("%d",&n)) {
int i = 2, res = 1;
if (n != 0)
while (i != 1) {
i <<= 1;
if (i > 2 * n)
i = i - 2 * n - 1;
res++;
}
cout << res << endl;
}
return 0;
}```

0条评论