//算法实现
#include <iostream>
#include <cstring>
using namespace std;
typedef struct Node
{
int num;
int code;
struct Node *next;
}NODE;
int n,m,len;
int resultnum[500];
int resultcode[500];
void input()
{
int i;
cout<<"请输入起始人数n : ";
cin>>n;
cout<<endl;
len = n;
cout<<"请输入m初值: ";
cin>>m;
cout<<endl;
return;
}
NODE* build()
{
int i;
NODE *p1,*p2,*head;
cout<<"请依次输入各人密码:"<<endl;
head = p2 = new NODE;
cin>>p2->code;
p2->num = 1;
for(i=2;i<=n;i++)
{
p1 = new NODE;
cin>>p1->code;
p1->num = i;
p2->next = p1;
p2 = p1;
}
p2->next = head;
return head;
}
void test(NODE *head)
{
NODE *p;
p = head;
while(p->next != head)
{
cout<<p->code<<" ";
p = p->next;
}
cout<<p->code;
return;
}
void game(NODE *head)
{
NODE *p,*pre;
int i,t=0;
p = head;
pre = head;
while(pre->next != head)
pre = pre->next;
while(len>1)
{
for(i=0;i<m-1;i++)
{
p = p->next;
pre = pre->next;
}
m = p->code;
pre->next = p->next;
cout<<endl<<"Out: "<<p->num<<endl<<"m : "<<m<<endl;
resultnum[t] = p->num;
resultcode[t] = p->code;
t++;
if(head == p)
head = pre->next;
delete p;
len--;
p = pre->next;
}
cout<<endl<<"Out: "<<pre->num<<endl<<"m : "<<pre->code<<endl;
cout<<endl<<endl<<"Result:"<<endl;
int k;
for(k=0;k<t;k++)
cout<<resultnum[k]<<" ";
cout<<pre->num<<endl;
for(k=0;k<t;k++)
cout<<resultcode[k]<<" ";
cout<<pre->code<<endl;
return;
}
int main()
{
NODE *head;
input();
head = build();
//test(head);
game(head);
return 0;
}
//可视化实现
#include <iostream>
#include <cstring>
#include<windows.h>
#include<cstdlib>
using namespace std;
typedef struct Node
{
int num;
int code;
struct Node *next;
}NODE;
int n,m,len;
int num[500];
int ncode[500];
int resultnum[500];
int resultcode[500];
void printspace(int a)
{
int i;
for(i = 0; i < a; i++)
cout<<" ";
}
void input()
{
int i;
cout<<"请输入起始人数n : ";
cin>>n;
cout<<endl;
len = n;
for(i=1;i<=n;i++)
num[i] = 1;
cout<<"请输入m初值: ";
cin>>m;
cout<<endl;
return;
}
NODE* build()
{
int i;
NODE *p1,*p2,*head;
cout<<"请依次输入各人密码:"<<endl;
head = p2 = new NODE;
cin>>p2->code;
ncode[1] = p2->code;
p2->num = 1;
for(i=2;i<=n;i++)
{
p1 = new NODE;
cin>>p1->code;
ncode[i] = p1->code;
p1->num = i;
p2->next = p1;
p2 = p1;
}
p2->next = head;
return head;
}
void test(NODE *head)
{
NODE *p;
p = head;
while(p->next != head)
{
cout<<p->code<<" ";
p = p->next;
}
cout<<p->code;
return;
}
void game(NODE *head)
{
NODE *p,*pre;
int i,j,t=0,k,spa=0;
p = head;
pre = head;
while(pre->next != head)
pre = pre->next;
while(len>1)
{
for(i=0;i<m;i++)
{
system("CLS");
cout<<"----------------------------------------------------------------------------"<<endl;
cout<<"Counter == "<<i+1<<endl<<endl;
cout<<"Number & Code:"<<endl;
for(j=1;j<=n;j++)
if(num[j]) cout<<j<<" ";
cout<<endl;
for(j=1;j<=n;j++)
if(ncode[j]) cout<<ncode[j]<<" ";
cout<<endl;
if(i)
{
p = p->next;
pre = pre->next;
}
printspace(spa);
cout<<"#";
spa = (spa + 1) % len;
Sleep(300);
}
m = p->code;
pre->next = p->next;
cout<<endl<<endl<<"Out: "<<p->num<<endl<<"m : "<<m<<endl;
num[p->num] = 0;
ncode[p->num] = 0;
if(len > 0)
{
if(p->next == head)
spa = 0;
else
spa--;
}
resultnum[t] = p->num;
resultcode[t] = p->code;
t++;
if(head == p)
head = pre->next;
delete p;
len--;
p = pre->next;
system("Pause");
}
system("CLS");
cout<<endl<<endl<<"Result:"<<endl;
for(k=0;k<t;k++)
{
//if(resultnum[k])
cout<<resultnum[k]<<" ";
}
cout<<pre->num<<endl;
for(k=0;k<t;k++)
{
//if(resultcode[k])
cout<<resultcode[k]<<" ";
}
cout<<pre->code<<endl;
return;
}
int main()
{
NODE *head;
input();
head = build();
//test(head);
game(head);
return 0;
}