您的位置:首页 > 其它

有N个人围成一圈做游戏,规则如下:从某一个人开始报数,依次报1,2,3,喊到3的人出局。

2017-11-25 16:22 393 查看
有N 个人围成一圈做游戏,规则如下:
从某一个人开始报数,依次报1,2,3,喊到3的人出局。
下一个人接着从1开始报数,直到最后只剩下一个人。
问最后剩下的是谁?
#include <iostream>
using namespace std;

int   who_win (const int&  num_of_players);

int main()
{
int num;
cin>>num;
int ret=who_win(num);
cout<<ret<<endl;
return 0;
}

int who_win(const int& num_of_players)
{
int* array=new int[num_of_players];
int* p=array;
for( int i=0;i<num_of_players;i++)
*(p+i)=i+1;

int i=0;
int index=0;
int out_person=0;

while(out_person+1<num_of_players)
{
if( *(p+i) )
index++;

if(index==3)
{
index=0;
*(p+i)=0;
out_person++;
}
i++;
if(i==num_of_players)
i=0;
}

while(!*p)
{
p++;
}

int ret=*p;
delete []array;
return ret;
}



或者用环形链表:

#ifndef JOSEFU_H_INCLUDED
#define JOSEFU_H_INCLUDED

#include <iostream>
#include<stdio.h>
using namespace std;

typedef struct node
{
int data;

struct node* next;

}node,*pnode;

pnode addback(pnode phead,int data);

pnode addfront(pnode phead,int data);

pnode deletefirst(pnode phead,int data);

pnode pdeletefirst(pnode phead,int data, pnode* p);

pnode finedata(pnode phead,int data);

pnode insertfirst(pnode phead,int finedata, int data);

pnode insertfirstback(pnode phead,int finedata, int data);

int getnum(pnode phead);

void showall(pnode phead);

#endif // JOSEFU_H_INCLUDED


#include "Josefu.h"
pnode addback(pnode phead,int data)
{
pnode pnew=new node;
pnew->data=data;
if(phead==NULL)
{
phead=pnew;
pnew->next=phead;
}
else
{
pnode p=phead;
while(p->next!=phead)
{
p=p->next;
}
p->next=pnew;
pnew->next=phead;
}
return phead;
}

pnode addfront(pnode phead,int data)
{
pnode pnew=new node;
pnew->data=data;
if(phead==NULL)
{
phead=pnew;
pnew->next=phead;
}
else
{
pnode p=phead;
while(p->next!=phead)
{
p=p->next;

}
p->next=pnew;
pnew->next=phead;
phead=pnew;
}
return phead;
}

pnode deletefirst(pnode phead,int data)
{
if(phead->next==phead)//判断只有一个节点的情况
{
if(phead->data==data)
{
delete phead;
return NULL;
}
}
else
{
pnode pre=NULL;
pnode cur=phead;
while(cur->next!=phead)//判断第一个节点到倒二个节点是否有相等值
{
if(cur->data==data)
{
if(pre==NULL)
{
while(cur->next!=phead)
{
cur=cur->next;
}
pnode r=phead;
phead=phead->next;
cur->next=phead;
delete r;

}
else
{
pre->next=cur->next;
delete cur;
}
return phead;
}
pre=cur;
cur=cur->next;
}
if(cur->data==data)//判断最后一个节点
{
pre->next=phead;
delete cur;
return phead;
}
}
}

pnode pdeletefirst(pnode phead,int data, pnode* p)
{
getnum(phead);
if(phead->next==phead)//判断只有一个节点的情况
{
if(phead->data==data)
{
delete phead;
*p=NULL;
return NULL;
}
}
else
{
pnode pre=NULL;
pnode cur=phead;
while(cur->next!=phead)//判断第一个节点到倒二个节点是否有相等值
{
if(cur->data==data)
{
if(pre==NULL)
{
while(cur->next!=phead)
{
cur=cur->next;
}
pnode r=phead;
phead=phead->next;
cur->next=phead;
delete r;
*p=phead;
}
else
{
pre->next=cur->next;
*p=cur->next;
delete cur;
}
return phead;
}
pre=cur;
cur=cur->next;
}
if(cur->data==data)//判断最后一个节点
{
pre->next=phead;
delete cur;
*p=phead;
return phead;
}
}
}

pnode finedata(pnode phead,int data)
{
pnode p=phead;
while(p->next!=phead)
{
if(p->data==data)
return p;
p=p->next;
}
if(p->data==data)
return p;
else
return NULL;
}

pnode insertfirst(pnode phead,int finedata, int data)//在finedata元素的前面插入data
{
pnode pnew=new node;
pnew->data=data;

pnode cur=phead;
pnode pre=NULL;
while(cur->next!=phead)
{
if(cur->data==finedata)
{

if(cur==phead)
{
pnode p=phead;
while(p->next!=phead)
{
p=p->next;
}
p->next=pnew;
pnew->next=phead;
phead=pnew;
}
else
{
pre->next=pnew;
pnew->next=cur;
}

return phead;
}
pre=cur;
cur=cur->next;
}
if(cur->data==finedata)
{
pre->next=pnew;
pnew->next=cur;
}

return phead;

}

pnode insertfirstback(pnode phead,int finedata, int data)//在finedata元素的后面插入data
{
pnode pnew=new node;
pnew->data=data;

pnode cur=phead;
pnode pre=NULL;
while(cur->next!=phead)
{
if(cur->data==finedata)
{

if(cur==phead)
{
pnew->next=phead->next;
phead->next=pnew;
}
else
{
pnew->next=cur->next;
cur->next=pnew;
}

return phead;
}
pre=cur;
cur=cur->next;
}
if(cur->data==finedata)
{
cur->next=pnew;
pnew->next=phead;
}

return phead;

}

void showall(pnode phead)
{
if(!phead) return;
pnode p=phead;
while(p->next!=phead)
{
printf("%6d,%6d,%2d\n",p,p->next,p->data);
p=p->next;
}
printf("%6d,%6d,%2d\n",p,p->next,p->data);
}

int getnum(pnode phead)
{
if(phead==NULL)
return 0;
else if(phead->next==phead)
return 1;
else
{
int count=1;
pnode save=phead;
while(save->next!=phead)
{
count++;
save=save->next;
}
return count;
}
}


#include "Josefu.h"
int main()
{
    #define N 10
    pnode phead=NULL;
    for( int i=0; i<N; i++)
        phead=addback(phead,i);
    showall(phead);
    printf("\n\n\n");

    pnode p=phead;
    while(getnum(phead)!=1)
    {
        for( int i=0; i<2; i++)
        {
            p=p->next;
        }
        phead=pdeletefirst(phead,p->data,&p);

    }
    showall(phead);
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  约瑟夫问题
相关文章推荐