| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1851 人关注过本帖
标题:问个数组的josephus问题
只看楼主 加入收藏
狂啃基础
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2004-10-13
收藏
 问题点数:0 回复次数:7 
问个数组的josephus问题

17个人围个圈报数,3的倍数的人离开,一直到最后一个人,然后求这个人的编号

程序如下:

#include <iostream.h> const int num=17; void main() { int interval=3; int a[num]; for(int m=0; m<num; m++) cout <<(a[m]=m+1) <<","; cout <<endl; int i=(interval-1)%num; for(int k=1; k<num; k++){ cout <<a[i] <<","; a[i]=0; for(int j=1; !(a[i]&&(j++==interval)); i=(i+1)%num); //数数 } cout <<"\nNo." <<a[i] <<" boy has won.\n"; //输出胜利者 }

程序我看的有点晕,特别是这句int i=(interval-1)%num 有点不太明白,请赐教~ 谢谢~

搜索更多相关主题的帖子: josephus 
2004-10-13 17:07
狂啃基础
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2004-10-13
收藏
得分:0 
一天了,怎么连个人影都没看见,这题应该难不到众斑竹吧?

2004-10-14 22:35
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

// 先把程序给你,也许我写的复杂了些,不过你的程序更本不是C++ 程序,只不过用了 cin, cout 而已。

// 今天,已经有朋友关于这个问题提问了,虽然,我没兴趣来做这种趣味题,不过考虑到,这道题和数据结构有点关系,

// 我还是做了。显然,你看都没看啊。你的这道题,只是条件特殊化了而已,如果,你认真研究一下,可以自己改写我的程序的,就像我现在做的一样。

#include <cstdlib> #include <ctime> #include <iostream> using namespace std;

struct Person { int secretNumber; int position; int current_position; Person * right; Person * left; };

class Josephu { private: int total; // How many persons total int n; // How many persons current Person * person; enum{ m = 3}; // at the begin we set the m value Person * current; public: Josephu(); // void set_the_number_of_persons(); bool createJosephuCircle(int i); // void set_m_value(); Person * bau_shu(const int m); void run_Josephu(); ~Josephu(); };

Josephu::Josephu() { total = 0; n = 0; person = NULL; current = NULL; }

bool Josephu::createJosephuCircle(int i) { total = n = i; person = new Person[total]; if(person == NULL) return false;

for(int j = 0; j<n; j++) { person[j].secretNumber = m; person[j].position = person[j].current_position = j; if(j == 0) { current = person; person[j].left = &person[j+1]; person[j].right = &person[n-1]; } else if(j == n-1) { person[j].left = &person[0]; person[j].right = &person[j-1]; } else { person[j].left = &person[j+1]; person[j].right = &person[j-1]; } } return true; } /* void Josephu::set_m_value() { cout<<"please enter the first m value.\n"; cout<<" m = "; cin>>m; } */ Person * Josephu::bau_shu(const int m) { Person * aus; Person * temp; Person * next; int offset = (m)%n?(m)%n-1:n-1; temp = current; for(int i = 0; i<offset; i++) { temp = temp->left; } aus = temp; current = aus->left; aus->right->left = aus->left; aus->left->right = aus->right; // m = aus->left->secretNumber;

next = aus->left; for(i = current->current_position; i<n; i++) { (next->current_position)--; next = next->left; } cout<<aus->position<<" "; n--; return current; } void Josephu::run_Josephu() { while(n != 1) { bau_shu(m); } cout<<"\nThe Winner is in position(Begin with the position 0) : "<<current->position<<endl; } Josephu::~Josephu() { if(person && total>1) delete [] person; else delete person; }

int main() { Josephu aJosephu; // aJosephu.set_the_number_of_persons(); aJosephu.createJosephuCircle(17); // create a Josephu Circle with 17 member // aJosephu.set_m_value(); // set the m value at the begin aJosephu.run_Josephu(); cout<<endl; system("pause"); return 0; }


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-10-14 23:13
狂啃基础
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2004-10-13
收藏
得分:0 

谢了,斑竹,不过我看的是钱能C++的书上程序啊,那不是C++是什么呢?

是不是程序有点乱和简化?


2004-10-15 10:07
corrupt
Rank: 2
等 级:新手上路
威 望:3
帖 子:535
专家分:0
注 册:2004-9-29
收藏
得分:0 

标准C++是要 运用 类的,用它的封装性。

楼主的 就和C一样,只是在输入和输出不同而已!!

斑竹我说的对不对啊??

-------------------

尽善尽美


2004-10-15 10:47
狂啃基础
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2004-10-13
收藏
得分:0 

呵呵 我知道拉 但是我还是初学者

面向对象还不熟悉~


2004-10-15 17:16
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
以下是引用corrupt在2004-10-15 10:47:11的发言:

标准C++是要 运用 类的,用它的封装性。

楼主的 就和C一样,只是在输入和输出不同而已!!

斑竹我说的对不对啊??

-------------------

尽善尽美

输入输出已经是C++了,cincout已经用到了重载运算符。
2004-10-15 20:11
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

我并不想批评谁,大家都有一个学习的过程。对象化编程与过程化编程有着本质的区别。但是从局部来看,任何对象化编程都离不开过程化编程。

学习C++, 很明显的是不再用 printf, scanf 了,取而代之的是 cin, cout. 当然这的确是用到了运算符重载,不过,从程序的整体来看,或者说,从编程的思维角度来看,真正的对象化编程是逃不掉 class 的。class 是对 对象的一个反映和体现阿。


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-10-16 00:38
快速回复:问个数组的josephus问题
数据加载中...
 
   



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

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