新第k人
http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4443
題解:經典的約瑟夫環問題
參考https://blog.csdn.net/tingyun_say/article/details/52343897
如果不一次處理完,重復操作可能超時
C++版本一
/* *@Author: STZG *@Language: C++ */ #include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<bitset> #include<queue> #include<deque> #include<stack> #include<cmath> #include<list> #include<map> #include<set> //#define DEBUGusing namespace std; typedef long long ll; const int N=10000+10; const double PI = acos(-1.0); const double EXP = 1E-8; const int INF = 0x3f3f3f3f; int t,n,m; int a[N]; int yuesefu(int n,int m){if(n == 1){return 0; //這里返回下標,從0開始,只有一個元素就是剩余的元素0}else{a[n-1]=yuesefu(n-1,m);return (a[n-1] + m) % n; //我們傳入的n是總共多少個數} }int main() { #ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endifscanf("%d",&t);a[10000]=yuesefu(10000,233);while(t--){scanf("%d",&n);cout<<a[n]+1<<endl;}//cout << "Hello world!" << endl;return 0; }C++版本二
題解:
因為一直k 是233,所有人站成一個環,如果知道了x 個站成一個環的時候,出去人的相對順序(最后一個出去的人的序列是1,第一個出去的人的編號是x),那么可以通過這個順序知道x+1 個人占成一個環的時候,出去的相對順序。即找到編號為x 的人,往前數232 個人和第233 個人之間插入一個位置,這個位置就是編號為x+1 的人。
#include<bits/stdc++.h> using namespace std;const int N = 1e5 + 10; int f[N]; int main() {f[1] = 0;for(int i = 2; i < N; ++i) f[i] = (f[i - 1] + 233) % i;int t;scanf("%d", &t);while(t--){int n;scanf("%d", &n);printf("%d\n", f[n] + 1);}return 0; }?
總結