Problem Joseph。。。lzl==sz

Posted by Cww97 on 2016-03-14

版权声明:本文为博主原创文章,未经博主允许不得转载。原文所在http://blog.csdn.net/cww97 https://blog.csdn.net/cww97/article/details/50885767
先写个链表版的,,,

dp版的以后再说

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <queue>
#include <cstdlib>
#include <iostream>
#include <cstdio>
using namespace std;
struct node{
int num;
node *next;
node (){num=0;next=NULL;}
};

int fun(int m,int n){
node *pre,*head=new node;
head->num=1;
pre=head;
for(int i=2;i<=n;i++){
node *cur=new node;
cur->num=i;
pre->next=cur;
pre=cur;
}
pre->next=head;
node *cur=head;
node *p=head,*q=head;
//work
for (int j=0;cur->next!=cur;cur=cur->next){
j++;
if (j%m==0) pre->next=cur->next;
pre=cur;
}
return cur->num;
}

int main(){
int a,b;
cout<<"请输入人数,报号数"<<endl;
cin>>a>>b;
cout<<"最后的胜利者是:NO."<<fun(b,a)<<endl<<"lzl==sz"<<endl;
return 0;
}