约瑟夫问题,程序死住了

wzuxian2012 2012-05-08 10:29:43

#include<list>

struct Jo
{
int pos;
bool flag;

Jo(int nPos, bool bFlag):pos(nPos),flag(bFlag){}
};



class ResolveJo
{
list<Jo> mylist;
int step;
int count;
private:
bool Check( int& nPos) const
{
list<Jo>::const_iterator iter=mylist.begin();

int n=0;

while(iter!= mylist.end())
{
if(iter->flag)
n++;
if(n==count-1)
{
nPos=iter->pos;
return true;
}
return false;
}

}
public:

ResolveJo(int nCount,int nStep)
{
count=nCount;
step=nStep;
for(int i=0; i<count ;i++)
mylist.push_back(Jo(i,true));
}


void Solution()
{
list<Jo>::iterator iter=mylist.begin();
int m=0;
int targetPos;
do
{
if(Check(targetPos)) //只剩下一人
{
cout<<targetPos<<endl;
break;
}
//continue
if(iter->flag)
m++;
if(m==step)
{
iter->flag=false;
}

} while (1);
}

};




int main()
{

ResolveJo obj(12,3);
obj.Solution();

return 0;
}




list一般是循环链表,所以可以用来解决约瑟夫问题,为什么程序死了。


问题2:


*iter.flag 这样使用为什么是错误的,*iter返回的是 list中存放的类型。既然是存放数据,就可以使用.了,但是

编译无法通过??


...全文
173 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
muyi66 2012-05-11
  • 打赏
  • 举报
回复
iter->flag 和 (*iter).flag 是一回事。只是*iter.flag是不存在的。
muyi66 2012-05-11
  • 打赏
  • 举报
回复
另一种写法:

#include "ring.h"
#include <iostream>

int main()
{
ring r;
for (int i=1;i<=21;++i)
r.push_back(i);
for (ring::iterator iter=r.begin(); r.Size()>1; iter=r.erase(iter))
{
for (int i=1;i<=4;++i)
++iter;
}
std::cout<<"最后留下来的是:"<<*(r.begin())<<" 号";
}
wzuxian2012 2012-05-11
  • 打赏
  • 举报
回复
楼上代码看懂了,谢谢了啊。

唯一的疑问就是 iter->flag

和 (*iter).flag

区别在哪里了??、
muyi66 2012-05-11
  • 打赏
  • 举报
回复
实际上自己写个类似STL的环形类也不难,上个月我曾经写了一个,用来编写约瑟夫环简单得很。

只是一直没时间去完善它,也没时间去改成模板。

用尚未完成的环形类写的21人约瑟夫环:

#include "ring.h"
#include <iostream>

int main()
{
ring r;
for (int i=1;i<=21;++i)
r.push_back(i);
ring::iterator iter=r.begin();
do
{
for (int i=1;i<=4;++i)
++iter;
iter=r.erase(iter);
}while(r.Size()>1);
std::cout<<"最后留下来的是:"<<*(r.begin())<<" 号";
}
muyi66 2012-05-11
  • 打赏
  • 举报
回复
我没见到那种List,不过只要它多了一个节点你就不能这样使用它。再说,不是别无选择也不该使用这种不可移植的特性。
wzuxian2012 2012-05-11
  • 打赏
  • 举报
回复
回复4楼,有的版本的list是双向循环,另外有个空节点,end,也就是说,如果果用了该版本,那么程序依然不

对, 因为约瑟夫问题,是没有空节点的,对吧??


回复2楼,让你该代码,谢谢了啊。

回复5楼,谢谢了啊,呵呵,不懂c

muyi66 2012-05-09
  • 打赏
  • 举报
回复
List不是循环链表,只是链表。用来解约瑟夫问题的话它没什么优势可言。

*iter才是存入的数据,iter不是。所以你该写成(*iter).flag
赵4老师 2012-05-09
  • 打赏
  • 举报
回复
//假设有n个人团团围做,从第1个人开始数数,数到第m个人时候,第m个人出列,
//然后继续从1开始数数,数到第m个人退出
#include <stdio.h>
#include <conio.h>
int i,k,t;
int n,m;
static char f[1001];//0该座位未出圈,1该座位已出圈
void main() {
while (1) {
printf("Input n m(1000>=n>=m>=1):");
fflush(stdout);
rewind(stdin);
if (2==scanf("%d%d",&n,&m)) {
if (1000>=n && n>=m && m>=1) break;
}
}
t=0;//已出圈总人数
i=1;//座位编号
k=1;//当前要数的数
while (1) {
if (0==f[i]) {
if (m==k) {
t++;
f[i]=1;
printf("%3d ",i);
if (0==t%10) printf("\n");
if (t>=n) break;
}
k++;if (k>m) k=1;
}
i++;if (i>n) i=1;
}
cprintf("Press any key ...");
getch();
}
W170532934 2012-05-08
  • 打赏
  • 举报
回复
更正下上面的程序。刚才没有检查,发现有错误。替换这个函数就可以了

bool Check( int& nPos) const
{
list<Jo>::const_iterator iter=mylist.begin();
int n=0;
while(iter!= mylist.end())
{
if(!iter->flag)
{
n++;

}
else
{
nPos = iter->pos;
}
iter++;

}
if(n==count-1)
{
return true;
}
return false;
}
W170532934 2012-05-08
  • 打赏
  • 举报
回复


struct Jo
{
int pos;
bool flag;

Jo(int nPos, bool bFlag):pos(nPos),flag(bFlag){}
};

class ResolveJo
{
list<Jo> mylist;
int step;
int count;
private:
bool Check( int& nPos) const
{
list<Jo>::const_iterator iter=mylist.begin();
int n=0;
while(iter!= mylist.end())
{
if(iter->flag)
n++;
if(n==count-1)
{
nPos=iter->pos;
return true;
}
return false;
}
}
public:

ResolveJo(int nCount,int nStep)
{
count=nCount;
step=nStep;
for(int i=0; i<count ;i++)
mylist.push_back(Jo(i,true));
}

void Solution()
{
list<Jo>::iterator iter=mylist.begin();
int m=0;
int targetPos;
do
{
if(Check(targetPos)) //只剩下一人
{
cout<<targetPos<<endl;
break;
}
//continue
if(iter==mylist.end())
iter = mylist.begin();
if(iter->flag)
m++;
if(m==step)
{
m = 0;
iter->flag=false;
cout<<iter->pos<<endl;
}
iter++;
} while (1);
}

};

int main()
{
ResolveJo obj(12,3);
obj.Solution();

return 0;
}

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧