SDUT3165:Round Robina(循环链表)
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=3165
题意分析:
比赛时这题没有A真伤心,错了11遍,最后发现题意少读了一句。
We note that E\'s count was irrelevant to the decision to end the game.
就是它丫的上面那句,比赛时真的是不能急着做题啊,一定要弄明白题在做,卡了三个小时就因为这个水逼题目。提供一组数据
4 5 输出时 3 1
代码如下:
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; typedef struct node { int fen; struct node *next; } node; int n,m; int main() { int cnt,sum,F,FF; while(scanf("%d",&n)!=EOF&&n!=0) { scanf("%d",&m); struct node *head,*tail,*p,*q; head=new node; head->fen=0; head->next=NULL; tail=head; for(int i=2; i<=n; i++) { p=new node; p->next=NULL; p->fen=0; tail->next=p; tail=p; } tail->next=head; for(q=head; q->next!=head;) q=q->next; sum=0; cnt=0; while(cnt<n-1) { p=q->next; p->fen++; sum++; if(sum%m==0) { sum-=m; F=q->fen; FF=p->next->fen; cnt++; if(F==FF) { break; } q->next=p->next; p->next=NULL; delete(p); } else q=p; } n=n-cnt; printf("%d %d\n",n,F); } return 0; }