C++ 约瑟夫环问题

C++ 约瑟夫环问题 

约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。

  1. /** 
  2. * 约瑟夫环: 
  3. * 已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。 
  4. * Josephu(int n, int m) 
  5. * @author arhaiyun 
  6. * Date: 2013-09-26 
  7. **/  
  8.   
  9. #include "stdafx.h"  
  10. #include <iostream>  
  11. #include <stdlib.h>  
  12.   
  13. using namespace std;  
  14.   
  15. typedef struct LinkNode{  
  16.     int index;  
  17.     struct LinkNode* next;  
  18. }JosephuNode;  
  19.   
  20.   
  21. int Josephu(int n, int m)  
  22. {  
  23.     //[1].Create a circulatory Link list  
  24.     JosephuNode *head, *tail;  
  25.     head = tail = (JosephuNode*) malloc(sizeof(JosephuNode));;  
  26.       
  27.     for(int i = 1; i < n; i++)  
  28.     {  
  29.         tail->index = i;  
  30.         tail->next = (JosephuNode*)malloc(sizeof(JosephuNode));  
  31.         tail = tail->next;  
  32.     }  
  33.     tail->index = n;  
  34.     tail->next = head;  
  35.       
  36.     //[1].Count to the specific number and let him out  
  37.     for(int i = 1; tail != head; ++i)  
  38.     {  
  39.         for(int j = 1; j < m; ++j)  
  40.         {  
  41.             tail = head;  
  42.             head = head->next;  
  43.         }  
  44.         tail->next = head->next;  
  45.         cout<<"The "<<i<<" round "<<head->index<<" is out"<<endl;  
  46.         free(head);  
  47.         head = tail->next;  
  48.     }  
  49.       
  50.     int winner = head->index;  
  51.     free(head);  
  52.     head = tail = NULL;  
  53.       
  54.     return winner;  
  55. }  
  56.   
  57.   
  58. int main()  
  59. {  
  60.     int winner = Josephu(5, 3);  
  61.     cout<<"Last winner is:"<<winner<<endl;  
  62.       
  63.     system("pause");  
  64.     return 0;  
  65. }  
/**
* 约瑟夫环:
* 已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。
* Josephu(int n, int m)
* @author arhaiyun
* Date: 2013-09-26
**/

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>

using namespace std;

typedef struct LinkNode{
	int index;
	struct LinkNode* next;
}JosephuNode;


int Josephu(int n, int m)
{
	//[1].Create a circulatory Link list
	JosephuNode *head, *tail;
	head = tail = (JosephuNode*) malloc(sizeof(JosephuNode));;
	
	for(int i = 1; i < n; i++)
	{
		tail->index = i;
		tail->next = (JosephuNode*)malloc(sizeof(JosephuNode));
		tail = tail->next;
	}
	tail->index = n;
	tail->next = head;
	
	//[1].Count to the specific number and let him out
	for(int i = 1; tail != head; ++i)
	{
		for(int j = 1; j < m; ++j)
		{
			tail = head;
			head = head->next;
		}
		tail->next = head->next;
		cout<<"The "<<i<<" round "<<head->index<<" is out"<<endl;
		free(head);
		head = tail->next;
	}
	
	int winner = head->index;
	free(head);
	head = tail = NULL;
	
	return winner;
}


int main()
{
	int winner = Josephu(5, 3);
	cout<<"Last winner is:"<<winner<<endl;
	
	system("pause");
	return 0;
}


 

顺便附上一个数学思想的约瑟夫环解法,要求有点不一样。就是一共n个人,查到m的人出圈,求最后圈里的人是几号。

  1. int fun(int n, int m)  
  2. {  
  3.     int i, r = 0;  
  4.     for (i = 2; i <= n; i++)  
  5.         r = (r + m) % i;  
  6.     return r+1;  
  7. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值