魔法师在玩一种扑克牌游戏,N张扑克分别标记为1~N。他打开第一张是1,把它放在一边,然后把最上面2张一张一张地依次移到最后;再打开上面一张刚好是2,再放到一边,把上面3张一张张依次移到最后;再打开上面一张刚好是3,如此继续,直到打开最后一张是N。
请问,这些扑克牌原来是如何排列的?
dalao代码qwq(100pts AC)
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,hh=1,tt=0,q[N],a[N],ans[N];
int main()
{
cin>>n;
for(int i=1;i<=n;i++) q[++tt]=i;
a[1]=q[hh++];
for(int i=2;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
tt=tt%n+1;
q[tt]=q[hh];
hh=hh%n+1;
}
a[i]=q[hh];
hh=hh%n+1;
}
for(int i=1;i<=n;i++) ans[a[i]]=i;
for(int i=1;i<=n;i++) printf("%d ",ans[i]);
return 0;
}
lzの代码(90pts AC 10pts TLE):
// #include <bits/stdc++.h>
#include <queue>
#include <stack>
#include <cmath>
#include <string>
#include <cstdio>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
inline int read() {
int res = 0, f = 1;
char k;
while (!isdigit(k = getchar())) if (k == '-') f = -1;
while (isdigit(k)) {
res = res * 10 + k - '0';
k = getchar();
}
return res * f;
}
const int NR = 1e5 + 5;
queue<int> q;
int qwq[NR], ans[NR];
int n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) q.push(i);
for (int i = 1; !q.empty(); i++) {
for (int j = 1; j <= i; j++) {
q.push(q.front());
q.pop();
}
qwq[i] = q.front();
q.pop();
}
for (int i = 1; i <= n; i++) ans[qwq[i]] = i;
for (int i = 2; i <= n; i++) cout << ans[i] << " ";
cout << ans[1] << ' ';
return 0;
}
这是什么原理?有大佬能解释一下吗?