⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 josephus问题.cpp

📁 C++Example实用的算法:包括枚举
💻 CPP
字号:
#include<iostream.h>
#include<iomanip.h>

struct josephus
{
	int code;
	josephus * next;
};

void main()
{
	int number;
	int interal;
	
	cout<<"Please input the number of children: ";
	cin>>number;
	cout<<"Please input the interal of counting: ";
	cin>>interal;
	
    josephus * ptrJose=new josephus[number];
	josephus * ptrCurrent=ptrJose;
	
	int count=0;
	
	cout<<"Now There are "<<number<<" in the queue:";
	for(int i=1;i<=number;i++)
	{
		ptrCurrent->next=ptrJose+i%number;
		ptrCurrent->code=i;
		ptrCurrent=ptrCurrent->next;
		if(count++ % 10==0)
			cout<<endl;
		cout<<setw(4)<<i;
	}
	cout<<endl<<endl;
    
	josephus * pivot;
	ptrCurrent=&ptrJose[number-1];
	
	while(ptrCurrent->next!=ptrCurrent)
	{
		for(int j=0;j<interal;j++)
		{
			pivot=ptrCurrent;
			ptrCurrent=ptrCurrent->next;
		}
		
		cout<<"NO."<<setw(4)<<ptrCurrent->code<<" is eliminated."<<endl;
		pivot->next=ptrCurrent->next;
		ptrCurrent=pivot;
	}
	cout<<endl;
	cout<<"The winner is "<<ptrCurrent->code<<endl<<endl;
	
	delete[] ptrJose;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -