跳到正文

学习·文化课

猴子选大王2

codingHe约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年1月25日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

【题目描述】


有n个猴子,每个猴子都有一个编号a[i]。

当i被淘汰了则i后第a[i]个会被淘汰

请输出淘汰顺序

【输入用例】 5 3 2 1 4 3 【输出用例】 3 4 5 1 2

代码如下

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct stu{
	int id;
	stu *next;
}*now,*head,*pev,*p;
int a[101];
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int n,m;
	cin>>n;
	head=new stu;
	head->id=1;
	head->next=NULL;
	now=head;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=2;i<=n;i++){
		p=new stu;
		p->id=i;
		p->next=NULL;
		now->next=p;
		now=p;
	}
	now->next=head;
	now=head;
	pev=head;
	int s=now->id;
	while(--n){
		for(int i=1;i<a[s];i++){
			pev=now;
			now=now->next;
		}
		cout<<now->id<<endl;
		s=now->id;
		pev->next=now->next;
		delete now;
		now=pev->next;
	}
	cout<<now->id;
	delete now;
	return 0;
}
/*
5 3 2 1 4 3
*/
原文