首页 > 代码库 > 华为机试—约瑟夫环

华为机试—约瑟夫环

输入人数n,开始位置s ,间隔数多少个m,输出出局序列


#include <iostream>
#include <vector>
using namespace std;

void Out(int n,int s,int m)
{
	vector<int> people;
	for (int i=1;i<=n;i++)
	{
		people.push_back(i);
	}
    int start = s-1;
	int tmp =1;
    int out;
	while (tmp <= n)
	{
        out=(start-1+m)%people.size();
		cout<<""<<tmp++<<"::"<<people[out]<<endl;
        people.erase(people.begin()+out);
		start=out;     
	}

}


int main()
{
	//n人数,s开始,m数多少个
	int n,s,m;
    cin>>n>>s>>m;
	Out(n,s,m);
	return 0;
}

测试结果,可能想的不周全,欢迎查漏补缺:

技术分享

华为机试—约瑟夫环