2746.cpp

来自「非常好的C++学习源码,里面包括各种算法的实现,二叉的的前中后序遍历等」· C++ 代码 · 共 62 行

CPP
62
字号
#include<iostream>
using namespace std;

struct a
{
	int p;
	a *next;
};

int main()
{
	a *link,*monkey,*mlast;
	int n,m,count,i;
	while (1)
	{
		cin>>n>>m;
		if (n==0)
			break;
		link=NULL;
		for (i=0;i<n;i++)
		{
			monkey=new a;
			monkey->p=i+1;
			if (link==NULL)
				link=mlast=monkey;
			else
			{
				mlast->next=monkey;
				mlast=monkey;
			}
		}
		mlast->next=link;
		count=1;
		if (m==1)
			cout<<n<<endl;
		else
		{
			while(link!=NULL)
			{
				if (link->next==link)
				{
					cout<<link->p<<endl;
					delete link;
					break;
				}
				if (count==m-1)
				{
					monkey=link->next;
					link->next=monkey->next;
					delete monkey;
					count=0;
				}
				link=link->next;
				count++;
			}
		}
	}
	return 0;
}
			

⌨️ 快捷键说明

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