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

有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;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haibianyoushark

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值