| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1020 人关注过本帖
标题:c++指针问题报数问题
只看楼主 加入收藏
风雨123
Rank: 2
等 级:论坛游民
帖 子:84
专家分:65
注 册:2013-2-23
结帖率:66.67%
收藏
已结贴  问题点数:12 回复次数:5 
c++指针问题报数问题
N个人围成一圈(编号分别为1,2,……,N,N<65535),从1号开始按照编号方向报数1、2、3,...n,凡报n的人离开,再从下一个人开始继续报数1、2、3..,n,报n的人又离开,反复进行此操作,请你编写程序,计算出最后留下的人的序号是多少?

输入:标准输入,第一个正整数为人的数目N值,第2个数为每次报数数,仅一行输入.

输出:使用一行输出你计算的最后留下来的人的标号.

样例:

标准输入:

5 3

标准输出:

4

求各位帮帮忙。
2013-03-07 21:24
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
收藏
得分:2 
简单的约瑟夫环问题,图书馆好多书都有这个列子,数组实现和链表实现的都有,你自己去看下,学习下不是很难的。

同学习......同进步....你帮我......我帮你.....上善若水.....
2013-03-08 00:01
Pirelo
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:118
专家分:550
注 册:2011-1-28
收藏
得分:10 
楼上正解:约瑟夫问题是很经典的问题(数组/链表+递归),附上我以前写的一个,链表实现,希望能帮助你理解并弄懂,否则只有害了你(顺便谴责一下自己这种直接贴出代码的不道德行为!)
程序代码:
struct node
{
    int index;
    int member;
    node * next;
};
//---------------Class chain begin---------------------
class chain
{
public:
    //constructor
    chain()
    {
        head = NULL;
        _chain_size = 0;
    }
    //destructor
    ~chain(){}

    //void create(int size);
    void create(int size, bool isCycleChain);
    void displayChain();
    node *searchByIndex(int index);
    node *searchByGivenElement(int target);
    void deleteByIndex(int index,bool isChangeHead);
    void deleteByGivenElement(int target);
    void reArrangeIndexAfterDelete();
    void reverseChain();
    void destroy();
    int getChainSize()
    {
        return _chain_size;
    }

private:
    node *head;
    int _chain_size;
};
    

void chain::create(int size, bool isCycleChain)
{
    node *pCreate=NULL;
    node *temp = NULL;
    for(int i =0;i<size;++i)
    {
        pCreate = new node;
        if(NULL == head)
        {
            cout<<"create the first node"<<endl;
            head = pCreate;
            temp = head;
            head->index = i+1;
            cin>>head->member;

            pCreate=NULL;
        }
        else
        {
            //not the first 
            //temp = temp->next; //error:" temp->next pointed to unknown address"
             //temp = pCreate; //error
            temp->next= pCreate;
            temp = temp->next;
            pCreate = NULL;
            temp->index = i+1;
            cin>>temp->member;
            if(size-1 == i)
            {
                //process the last node:
                cout<<"create the last node\n";
                if(true == isCycleChain)
                {
                    //if a cycle chain, the last node point to the first node:
                    temp->next = head;
                }
                else
                {
                    // a single-direction chain
                    temp->next = NULL;//the last node's next should be NULL
                }
            }
        }
    }
    _chain_size = size;
    return;
}
    
void chain::destroy()
{
    if(NULL == head) return;
    node *pDel = head;
    for(int i=0;i<_chain_size;i++)
    {
        head = head->next;
        delete pDel;
        pDel = head;
    }
    cout<<"destroy complete"<<endl;
    pDel = NULL;
    head = NULL;
    _chain_size = 0;

    return;
}

void chain::displayChain()
{
    node *p = head;
    for(int i=0;i<_chain_size;i++)
    {
        cout<<"index = "<<p->index<<": "<<p->member<<endl;
        p = p->next;
    }
    p = NULL;
    return;
}
node *chain::searchByIndex(int index)
{
    node *temp = head;
    if(temp->index == index) return temp;// when searching the first node
    for(int i=0;i<_chain_size;i++)
    {
        if(NULL == temp->next)
        {
            //pointer is 0, this might be an error condition, or the last node of non-cycle
            return temp;
        }
        if(temp->next->index == index)
        {
            //return the previous node of target, search from the second node, end with the fitst node
            return temp;
        }
        else
        {
            temp = temp->next;
            continue;
        }
    }

    cout<<"index = "<<index<<" is invalid "<<endl;
    return NULL;
}

void chain::deleteByIndex(int index, bool isChangeHead)
{
    node *delOfPrevious = searchByIndex(index);
    node *del = NULL;
    if(NULL == delOfPrevious)
    {
        cout<<"target not found"<<endl;
        return;
    }

    del = delOfPrevious->next;//target node to be delete
    delOfPrevious ->next = del->next; //connect first
    int targetToBeDelete = del->member;
    if(isChangeHead)
    {
        //head will be changed
        head = del->next;
    }
    delete del;
    del = NULL;
    _chain_size--;
    cout<<"Deleted member is:  "<<targetToBeDelete<<endl;
    return;
}
void chain::reArrangeIndexAfterDelete()
{
    node *temp = head;
    for(int i=0;i<_chain_size;i++)
    {
        temp->index = i+1;
        temp = temp->next;
    }
    cout<<"index of all element updated"<<endl;
    return;
}
//---------------Class chain end-----------------------
//--------------------Josef begin----------------------
void recursion_Josef(chain *pChain, int m)
{
    if(0 == m) return;
    int size=pChain->getChainSize();
    if(size == 1) return;
    if(size>=m)
    {
        pChain->deleteByIndex(m,true);
    }
    else
    {
        pChain->deleteByIndex(m%size,true);
    }
    pChain->reArrangeIndexAfterDelete();
    pChain->displayChain();
    //recursion
    recursion_Josef(pChain,m);
    return;
}
void test_Josef()
{
    chain Chain;
    chain *pChain = NULL;
    int m,n;
    cout<<"input m,n respectively:"<<endl;
    cin>>m>>n;

    Chain.create(n,true);
    pChain = &Chain;
    Chain.displayChain();
    recursion_Josef(pChain, m);
    cout<<"the last one remain is:"<<endl;
    pChain->displayChain();
    Chain.destroy();
    return;
}
//--------------------Josef end------------------------
//--------------------main() begin---------------------
void main()
{
    test_Josef();
    return;
}
//--------------------main() end-----------------------
2013-03-08 13:08
风雨123
Rank: 2
等 级:论坛游民
帖 子:84
专家分:65
注 册:2013-2-23
收藏
得分:0 
#include <iostream>
using namespace std;
int main()
{
    int n,m,a[100],*p,k,l;
    cin>>n>>m;
    for(i=0;i<n;i++)
        *(p+i)=i+1;
    j=0;
    i=0;
    k=0;
    while(j<n-1)
    {
        if(*(p+i)!=0)
        {
            k++;
        }
        if(k==m)
        {
            cout<<*(p+i)<<endl;
            j++;
            k=0;
           *(p+i)=0;

        }
        i++;
        if(i==n)
        {
          i=0;
        }
    }
    cout<<*p<<endl;
    return 0;
}

哪里错了?????编不出来
2013-03-08 13:59
风雨123
Rank: 2
等 级:论坛游民
帖 子:84
专家分:65
注 册:2013-2-23
收藏
得分:0 
谢谢各位,我知道怎么做了
2013-03-08 15:56
风雨123
Rank: 2
等 级:论坛游民
帖 子:84
专家分:65
注 册:2013-2-23
收藏
得分:0 
回复 3楼 Pirelo
谢谢祢的好意,我还没学到哪里来
2013-03-08 15:57
快速回复:c++指针问题报数问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022421 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved