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

📄 yuesefu0.cpp

📁 用c++语言开发的求解著名的约瑟夫问题的程序
💻 CPP
字号:
#include"iostream.h"
const int MAX=100;
class SeqList
{
public:
	int data[MAX];
	int len;
	SeqList() { len=0; }
	void InsertList(int i, int x);
	void DeleteList(int i);
};
void SeqList::InsertList(int i, int x)
{
	if(i<1||i>len+1||len==MAX)
		cout<<"insert error or full!";
	else
	{
		for(int j=len-1;j>=i-1;j--)
			data[j+1]=data[j];
	}
	data[i-1]=x;
	len++;
}
void SeqList::DeleteList(int i)
{
	if(i<1||i>len)
		cout<<"the num. is not existent";
	else 
	{
		cout<<data[i-1]   <<endl;
		for(int j=i;j<=len-1;j++)
			data[j-1]=data[j];
	}
	len--;
}
void main()
{
   SeqList yuesefu;
   int n, s, m,p;
   cout<<"Input n,s,m:";
   cin>>n>>s>>m;
   for(int i=1;i<=n;i++)
	   yuesefu.InsertList(i,i);
   p=s+m-1;
   while( yuesefu.len!=0)
   {
	  
	   if(p%(yuesefu.len)!=0)
		   p=p%(yuesefu.len);
	   else
		   p=yuesefu.len;
        yuesefu.DeleteList(p); 
	p=p+m-1;
   }
}





	
	

⌨️ 快捷键说明

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