约瑟夫环问题

网友投稿 249 2022-12-02

约瑟夫环问题

最近做比赛遇到了所谓的约瑟夫环问题;

将之前做过的相关的题做一个总结:

搜狗百科:​​http://baike.sogou.com/v7958290.htm​​:

约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。通常解决这类问题时把编号从0~n-1,最后结果+1即为原问题的解。

约瑟夫环运作如下:

1、一群人围在一起坐成环状(如:N)

2、从某个编号开始报数(如:K)

3、数到某个数(如:M)的时候,此人出列,

4、一直循环,直到所有人出列,

NYOJ287--​​http://acm.nyist.net/JudgeOnline/problem.php?pid=278​​

排队

3000  ms  |  内存限制: 65535

1

第一行是n,有n次游戏,第二行是m,x,表示某次游戏有m个人,指定被请出列的数字为x。其中n<100,m<1000 输出 最后幸存的那位的原来的号码 样例输入

2 10 5 6 4

样例输出

3 5

约瑟夫环问题的实例化: 参考代码:

#include#include#include#include#include#include#include#include#includeusing namespace std;#define Max(a,b) a>b?a:b#define Min(a,b) a>b?b:a#define mem(a,b) memset(a,b,sizeof(a))int dir[4][2]= {{0,-1},{-1,0},{0,1},{1,0}};int main(){

int t,a,b; scanf("%d",&t); while(t--) { int ans=0; scanf("%d%d",&a,&b); for(int i=2;i<=a;i++) ans=(ans+b)%i; printf("%d\n",ans+1); } return 0;}

HDOJ2925:​​Chairs

In the traditional game of Musical Chairs, N + 1 children run around N chairs (placed in a circle) as long as music is playing. The moment the music stops, children run and try to sit on an available chair. The child still standing leaves the game, a chair is removed, and the game continues with N children. The last child to sit is the winner.

In an attempt to create a similar game on these days' game consoles, you modify the game in the following manner: N Children are seated on N chairs arranged around a circle. The chairs are numbered from 1 to N . Your program pre-selects a positive number D . The program starts going in circles counting the children starting with the first chair. Once the count reaches D , that child leaves the game, removing his/her chair. The program starts counting again, beginning with the next chair in the circle. The last child remaining in the circle is the winner.

For example, consider the game illustrated in the figure above for N = 5 and D = 3 . In the figure, the dot indicates where counting starts and × indicates the child leaving. Starting off, child #3 leaves the game, and counting restarts with child #4. Child #1 is the second child to leave and counting restart with child #2 resulting in child #5 leaving. Child #2 is the last to leave, and child #4 is the winner. Write a program to determine the winning child given both N and D .

Input

Your program will be tested on one or more test cases. Each test case specifies two positive integers N and D on a single line, separated by one or more spaces, where N, D < 1, 000, 000 . The last line of the input file contains two 0's and is not part of the test cases.

Output

For each test case, write the winner using the following format: N D W Where N and D are as above, is a space character, and W is the winner of that game.

Sample Input

5 3 7 4 0 0

Sample Output

5 3 4 7 4 2

Source

​​2008 ANARC​​

题意:大致如所上述:

代码:

#include#include#include#include#include#include#include#include#includeusing namespace std;#define Max(a,b) a>b?a:b#define Min(a,b) a>b?b:a#define mem(a,b) memset(a,b,sizeof(a))int dir[4][2]= {{0,-1},{-1,0},{0,1},{1,0}};const double Pi = acos(-1.0);const int maxn=305;const double eps=1e-10;char a[50],b[50];int fun(int n,int m){ int i,ans=0; for(i=2; i<=n; i++) { ans=(ans+m)%i; } return ans;}int main(){ int n,m,i,j,ans; while(~scanf("%d%d",&n,&m)&&n!=0&&m!=0) { printf("%d %d %d\n",n,m,fun(n,m)+1); } return 0;}

综上两种方法解决:

解法一(From Net):      思想:归纳为数学性问题。

无论是用链表实现还是用数组实现都有一个共同点:要模拟整个游戏过程,不仅程序写起来比较烦,而且时间复杂度高达O(nm),当n,m非常大(例如上百万,上千万)的时候,几乎是没有办法在短时间内出结果的。我们注意到原问题仅仅是要求出最后的胜利者的序号,而不是要读者模拟整个过程。因此如果要追求效率,就要打破常规,实施一点数学策略。为了讨论方便,先把问题稍微改变一下,并不影响原意:问题描述:n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数。求胜利者的编号。我们知道第一个人(编号一定是m%n-1) 出列之后,剩下的n-1个人组成了一个新的约瑟夫环(以编号为k=m%n的人开始):  k  k+1  k+2  ... n-2, n-1, 0, 1, 2, ... k-2并且从k开始报0。现在我们把他们的编号做一下转换:k     --> 0k+1   --> 1k+2   --> 2......k-2   --> n-2k-1   --> n-1变换后就完完全全成为了(n-1)个人报数的子问题,假如我们知道这个子问题的解:例如x是最终的胜利者,那么根据上面这个表把这个x变回去不刚好就是n个人情况的解吗?!!变回去的公式很简单,相信大家都可以推出来:x'=(x+k)%n如何知道(n-1)个人报数的问题的解?对,只要知道(n-2)个人的解就行了。(n-2)个人的解呢?当然是先求(n-3)的情况 ---- 这显然就是一个倒推问题!好了,思路出来了,下面写递推公式:令f[i]表示i个人玩游戏报m退出最后胜利者的编号,最后的结果自然是f[n]递推公式f[1]=0;f[i]=(f[i-1]+m)%i;  (i>1)有了这个公式,我们要做的就是从1-n顺序算出f[i]的数值,最后结果是f[n]。因为实际生活中编号总是从1开始,我们输出f[n]+1由于是逐级递推,不需要保存每个f[i],程序如上述。

解法二:

思考怎么用链表实现。

思想:建立一个有N个元素的循环链表,然后从链表头开始遍历并记数,如果计数i==m(i初始为1)踢出元素,继续循环,当当前元素与下一元素相同时退出循环。代码:

/* 约瑟夫环问题(Josephus) 用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出。写出C程序。(约瑟夫环问题 Josephus)*/#include #include // 链表节点typedef struct _RingNode{ int pos; // 位置 struct _RingNode *next;}RingNode, *RingNodePtr;// 创建约瑟夫环,pHead:链表头指针,count:链表元素个数void CreateRing(RingNodePtr pHead, int count){ RingNodePtr pCurr = NULL, pPrev = NULL; int i = 1; pPrev = pHead; while(--count > 0) { pCurr = (RingNodePtr)malloc(sizeof(RingNode)); i++; pCurr->pos = i; pPrev->next = pCurr; pPrev = pCurr; } pCurr->next = pHead; // 构成环状链表}void PrintRing(RingNodePtr pHead){ RingNodePtr pCurr; printf("%d", pHead->pos); pCurr = pHead->next; while(pCurr != NULL) { if(pCurr->pos == 1) break; printf("\n%d", pCurr->pos); pCurr = pCurr->next; }}void KickFromRing(RingNodePtr pHead, int m){ RingNodePtr pCurr, pPrev; int i = 1; // 计数 pCurr = pPrev = pHead; while(pCurr != NULL) { if (i == m) { // 踢出环 printf("\n%d", pCurr->pos); // 显示出圈循序 pPrev->next = pCurr->next; free(pCurr); pCurr = pPrev->next; i = 1; } pPrev = pCurr; pCurr = pCurr->next; if (pPrev == pCurr) { // 最后一个 printf("\n%d", pCurr->pos); // 显示出圈循序 free(pCurr); break; } i++; }}int main(){ int m = 0, n = 0; RingNodePtr pHead = NULL; printf("---------------Josephus Ring---------------\n"); printf("N(person count) = "); scanf("%d", &n); printf("M(out number) = "); scanf("%d", &m); if(n <= 0 || m <= 0) { printf("Input Error\n"); system("pause"); return 0; } // 建立链表 pHead = (RingNodePtr)malloc(sizeof(RingNode)); pHead->pos = 1; pHead->next = NULL; CreateRing(pHead, n);#ifdef _DEBUG PrintRing(pHead);#endif // 开始出圈 printf("\nKick Order: "); KickFromRing(pHead, m); printf("\n"); system("pause"); return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Mybatis 插件原理解析
下一篇:浅谈C/C++的time_t函数
相关文章

 发表评论

暂时没有评论,来抢沙发吧~