在一行中输入三个整数
,用空格隔开。
输出一个整数,表示最后剩下的"大王"编号。
5 1 2
3
初始队列编号为,从编号
开始报数:
出队,剩余
;
出队,剩余
;
出队,剩余
;
出队,剩余
,输出
。
#include <iostream>
using namespace std;
int main() {
int n,k,m; // 人数,开始位置,报数步长
cin>>n>>k>>m;
int a[n]; // 标记是否出列
int count=n;
int cnt,num=k; // num = k,用于标记当前报数人的位置
for(int i=0;i<n;i++){
a[i]=i; // 初始化数组,每个人都有自己的编号
}
cnt=0; // 计数器,当前报数的步数
while (count>=1) { // 还有未出列的人就循环
if(a[num]!=-1) { // 若当前报数人未出列
cnt++; // 则报数计数器+1,表示有人报数
if(cnt==m) { // 若数到了m!
a[num]=-1; // 当前人出列
count--; // 剩余人数--
cnt=0; // 报数计数归零!
}
}
if(num==n) num=1; //假设转了一圈,则从头开始再来
else num++; //
}
cout<<num-1<<endl;
} #include <iostream>
using namespace std;
#include <list>
#include <iterator>
int main() {
int n,k,m;
cin >> n >> k >> m;
list<int> lst;
int count = 0;
for(int i = 0; i < n; ++i)
{
lst.push_back(i);
}
list<int>::iterator curr = lst.begin();
advance(curr, k);
while(lst.size() > 1)
{
++count;
if(count == m)
{
curr = lst.erase(curr);
if(curr == lst.end()) {curr = lst.begin();}
count = 0;
}
else
{
++curr;
if(curr == lst.end()) {curr = lst.begin();}
}
}
cout << *lst.begin() << endl;
}
#include <stdio.h>
int main()
{
int n, k, m;
scanf("%d %d %d", &n, &k, &m);
int arr[n],a=0,p=k;//arr[]s数组标记0出队,a标记出队人数,p标记当前未出队的首个报数编号
for(int i=0;i<n;i++) arr[i] = 1;
while(n-1-a)
{
for(int j=0;j<m-1;j++)
{
do{
if(++p==n) p=0;
}while(!arr[p]);//找出出队编号
}
arr[p]=0;
a++;
do{
if(++p==n) p=0;
}while(!arr[p]);//找出出队后下个首个报数编号
}
printf("%d\n",p);
return 0;
} #include <stdio.h>
int main() {
int n, k, m;
if (scanf("%d %d %d", &n, &k, &m) != 3 || n < 2 || n > 100 || k < 0 || k >= n || m < 1 || m > 100) {
return 1;
}
int N[n], t = m - 1;
for (int i = 0; i < n; i += 1) {
N[i] = i;
}
while (n > 1) {
k = (k + t) % n;
n -= 1;
for (int i = k; i < n; i += 1) {
N[i] = N[i + 1];
}
}
printf("%d", *N);
return 0;
} #include <cassert>
#include <iostream>
#include <list>
#include <vector>
int method_1(int n, int k, int m) {
std::vector<int> people;
for (int i = 0; i < n; ++i) {
people.push_back(i);
}
int cur_idx = k;
while (people.size() > 1) {
int elim_idx = (cur_idx + m - 1) % people.size();
people.erase(people.begin() + elim_idx);
cur_idx = elim_idx % people.size();
}
return people[0];
}
int method_2(int n, int k, int m) {
std::list<int> people;
for (int i = 0; i < n; ++i) {
people.push_back(i);
}
auto it = people.begin();
advance(it, k);
while (people.size() > 1) {
int steps = (m - 1) % people.size();
while (steps--) {
if (++it == people.end())
it = people.begin();
}
it = people.erase(it);
if (it == people.end())
it = people.begin();
}
return people.front();
}
int main() {
int n, k, m;
std::cin >> n >> k >> m;
assert(n >= 2 && n <= 100);
assert(k >= 0 && k <= n - 1);
assert(m >= 1 && m <= 100);
// std::cout << method_1(n, k, m) << "\n";
std::cout << method_2(n, k, m) << "\n";
return 0;
} #include <stdio.h>
#define MAX 105
int main() {
int n=0,k=0,m=0;
int arr[MAX]={0};
scanf("%d%d%d",&n,&k,&m);
if(n>=2&&n<=100&&k>=0&&k<=n-1&&m>=1&&m<=100)
{
int count=n;//剩余人数
int cur=k;
for(int i=0;i<n;i++)
{
arr[i]=i;
}
while(count>1)
{
int step=0;//当前步数
while(step<m)
{
if(arr[cur]!=-1)//出队的人的下标标为-1
{
step++;
}
if(step<m)
{
cur=(cur+1)%n;
}
}
//报数到m了出队
arr[cur]=-1;
count--;
while(arr[cur]==-1)
{
cur=(cur+1)%n;//重新报数位置
}
}
for(int i=0;i<n;i++)
{
if(arr[i]!=-1)//最终数组里面只剩一个不为-1的元素就是大王了
{
printf("%d",arr[i]);
break;
}
}
}
return 0;
} #include <stdio.h>
int main() {
int n = 0, k = 0, m = 0, count = 0, step = 0, current;
int people[1000];
//count是移除的人
//step是报的数
//current是现在的位置
scanf("%d %d %d", &n, &k, &m);
current = (k - 1 + n) % n;
for (int i = 0; i < n; i++) {
people[i] = 1;
}//定义一个全是1的,个数为n的数组,1是还在队伍里面,0是已经出了队伍
while (count < n -
1) { //让移除的人还没到n的时候循环,一直持续到只有一个人在队伍里面
//开始报数
while (step < m) { //另起一个报数的计数器step
if (people[current] == 1)
step++;//如果这个人报数还没有到m,那么直接跳过他,报数计数器+1
if (step < m)
current = (current + 1) % n;
}
//一轮报数结束,开始换下一组
//清除计数器,把移除的人标记成0
step = 0;
people[current] = 0;
count++;
//开始下一个
while (people[current] == 0)
current = (current + 1) %
n;//取模就相当于让最后一个直接跳到第一个,且不用担心溢出问题
}
for (int i = 0; i < n; i++) {
if (people[i] == 1)
printf("%d", i + 1);
}
return 0;
}