PROBLEM F: 用链表实现约瑟夫环

Description

你听说过约瑟夫问题吗?问题大致如下:首先n个人围成一个圈,标记为1到n号。接着,从1号开始报数(从1开始),然后2号报数,然后3号。。。当有人报到到m时,这个人就要踢出比赛,然后从被踢出的人的下一个人开始,重新报数(从1开始)。这样经过n-1次后,就只剩下了一个人,问最后剩下的那个人是几号?

Input

第1行为T,表示有T组数据;

第2行到第T+1开始,每行输入n和m,n表示有几个人,m为上述的每报数m次就要踢出一个人

1=<n<=100, 1=<m<=100

Output

一个数,表示最后剩下了几号     

Sample Input

2
5 3
6 4

Sample Output

4
5

思路:emm用vector模拟的

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5;
typedef long long LL;

int main(void)
{
	LL T;cin>>T;
	vector<int>table;
	while(T--)
	{
		table.clear();
		LL n,m;cin>>n>>m;
		for(LL i=0;i<n;i++) table.push_back(i);
		LL pos=0;
		while(table.size())
		{
			pos=(pos+m-1)%table.size();
			table.erase(table.begin()+pos); 
		}	
		cout<<table[0]+1<<endl;	
	}
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/106870325