日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

新第k人

發布時間:2024/10/5 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 新第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; }

?

總結

以上是生活随笔為你收集整理的新第k人的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。