Visitors hit counter dreamweaver

数据结构 Joseph

  初始值M=20.输入N个人,每个人手里拿有一个密码。出去的那个人,他手上的密码就为M值。。求他们出来的顺序。

#include <iostream>
#include <malloc.h>
int initM=20; // THe initial M
using namespace std;
typedef struct LNode{
int no;
int code;
struct LNode *next;
}LNode,*LinkList;
void InitList(LinkList &L){
if(L==NULL){
L=(LinkList)malloc(sizeof(LNode));
L->no=0;
L->code=0;
L->next=L;
}
}
void ListInsert_L(LinkList &L,int e,int co){
LinkList p, s;
p=L;
if(p->no==0)
{
p->no=e;
p->code=co;
p->next=p;
}
else{
s=(LinkList)malloc(sizeof(LNode));
s->no=e;
s->code=co;
s->next=p->next;
p->next=s;
p=s;
}
L=p; //L always point to the end
}

void Joseph(LinkList &L,int n){
int count=0;
int flag=0,i=0; //flag=0 means the first turn,M=20
LinkList p;
for(p=L->next;i<=n;p=p->next){
if(p->code!=0) //It hasn't been visited
count++;
if(flag==0 && count==initM){
cout<<p->no<<"";
initM=p->code;
p->code=0;
flag=1;
count=0;
i++;
}
if(flag==1 && count==initM){
cout<<p->no<<"";
initM=p->code;
p->code=0;
i++;
count=0;
}
}
}


int main()
{
LinkList L=NULL;
InitList(L);
freopen("acm.txt","r",stdin);
int n;
cin>>n;
int i,j=1;
while(cin>>i){
ListInsert_L(L,j,i);
j++;
}
/*
LinkList p;
for( p=L;p->next!=L;p=p->next)
cout<<p->no<<" "<<p->code<<" "<<endl;
cout<<p->no<<" "<<p->code<<" "<<endl;
*/
Joseph(L,n);
return 0;
}



posted @ 2011-11-22 22:50  Jason Damon  阅读(392)  评论(0编辑  收藏  举报