题目描述
Musical chairs is a game frequently played at children’s parties. Players are seated in a circle facing outwards. When the music starts, the players have to stand up and move clockwise round the chairs. One chair is removed, and when the music stops the players all have to try to sit down on one of the chairs. The player who does not manage to sit down is out, and the game continues until there is just one player left, who is the winner.
输入
The first line contains a single integer, N which is the number of players (1 < N <= 15).
The next N lines have the names of the players.
The next line contains R, an integer which tells how many rounds are to be processed (0 < R < N). The next R lines each contain a pair of integers S and M, separated by a space. S is the number of the seat to be removed. Seats are numbered from 1 to the number of seats remaining in a clockwise direction.
M is the number of moves made before the music stops (0 < M <= 30). A move takes a player from one seat to the next in a clockwise direction. A move from the highest seat number takes a player to seat 1.
输出
After each round has taken place, output a line
<name> has been eliminated.
where <name> is the name of the person who does not find a seat. This will be the person who would have ended up at the seat which was removed.
At the end of the specified number of moves, output a line which either says
<name> has won.
where a single player remains, or
Players left are <name list>.
where the game is not yet finished.
<name list> contains the name of each player not yet eliminated in the same order as in the input. The names are separated by spaces.
样例输入
5 Anne Bill Chen Di Everet 4 3 6 2 8 1 5 2 6
样例输出
Bill has been eliminated. Anne has been eliminated. Chen has been eliminated. Everet has been eliminated. Di has won.
思路分析:
抢椅子游戏,就是我们经常玩的游戏,题意挺容易理解的,让我们计算出谁是胜利者,或者最后还有几个人在场上。但是有一些坑,最后如果还有好几个人还在场上的话,要输出“Players left are ”,加上在场上的人名,最后还有一个点(英文点),就这地方卡了我们好几次。写的话挺难模拟的,一般都用的是队列,我用的是map,存的是人名,等于他当前的编号,每去掉一个人就把值赋成-1,并且要遍历一次,把大于这个数的值减减,并且总数减减(是为了取余的),遍历的n不用减减,这样就跑出来了。
AC代码
#include<bits/stdc++.h>
#define Xuboy main
using namespace std;
const int maxn=1e6+5;
string ans[1010];
struct node{
string a;
}edge[1010];
map<string,int>mp;
int Xuboy()
{
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>edge[i].a;
mp[edge[i].a]=i;
}
int m,mod;
mod=n;
cin>>m;
for(int i=0;i<m;i++){
int b,c;
cin>>b>>c;
for(int j=1;j<=n;j++){
if(mp[edge[j].a]!=-1){
mp[edge[j].a]=(mp[edge[j].a]+c)%mod;
if(mp[edge[j].a]==0){
mp[edge[j].a]=mod;
}
}
}
for(int j=1;j<=n;j++){
if(mp[edge[j].a]==b){
cout<<edge[j].a;cout<<" has been eliminated."<<endl;
mp[edge[j].a]=-1;
}
}
int cnt=1;
for(int j=1;j<=n;j++){
if(mp[edge[j].a]!=-1&&mp[edge[j].a]>b)
mp[edge[j].a]-=1;
}
mod--;
}
int sign=0;
for(int i=1;i<=n;i++){
if(mp[edge[i].a]!=-1){
sign++;
}
}
if(sign==1){
for(int i=1;i<=n;i++){
if(mp[edge[i].a]!=-1){
cout<<edge[i].a<<" ";
cout<<"has won."<<endl;
}
}
}
else{
int p=0;
cout<<"Players left are ";
for(int i=1;i<=n;i++){
if(mp[edge[i].a]!=-1){
if(p==0)
cout<<edge[i].a;
else
cout<<" "<<edge[i].a;
p++;
}
}
cout<<"."<<endl;
}
return 0;
}