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

📄 num.cpp

📁 小孩排队问题
💻 CPP
字号:
#include <iostream.h>
#include <math.h>
void main()
{
	int m,l,n;
	cout<<"输入n:";
	cin>>n;	
	int *a;
	a=new int [n];             //定义数组
	for(l=1;l<=n;l++)
		a[l]=l-1;              //为小孩赋编号;
	cout<<"输入m:";
	cin>>m;
	for(l=1;l<=n;l++)
		cout<<a[l]<<",";
	cout<<endl;
	int k=1;
	int i=0;
	while(1)
	{
		for(int j=1;j<=m;)            //每次记数m次;
		{
			i=(i+1)%(n+1);            //越界检查;
			if(i==0)
				i=1;
			if(a[i]!=-1)              //在队伍里则记数一次;
				j++;
		}
		if(k==n) break;
		cout<<a[i]<<",";               //第m个人出队;
		a[i]=-1;                       //赋值为-1表示出队;
		k++;
	}
	cout<<"\nNo."<<a[i]<<" is the last one."<<endl;
}
	

⌨️ 快捷键说明

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