yuesefu0.cpp

来自「用c++语言开发的求解著名的约瑟夫问题的程序」· C++ 代码 · 共 63 行

CPP
63
字号
#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 + =
减小字号Ctrl + -
显示快捷键?