■ ジョセファスの問題
24ページ.
N人が集団自殺しようとしていて,全員が円陣に並び,その円の中のM番目の人を処刑する.一人死ぬと円の大きさは1減る.処刑される順番を列挙.循環リストを利用.
■ josephus_problem.cpp
#include <iostream> #include "Node.h" using namespace std; using namespace algo; /** * ジョセファスの問題 * P.24 */ void josephus_problem() { int N, M; Node<int> *t, *x; cout << "Enter the total number of people(N) : "; cin >> N; cout << "Who attempts suicide first?(M) : "; cin >> M; t = new Node<int>; t->key = 1; x = t; for (int i = 2; i <= N; i++) { t->next = new Node<int>; t = t->next; t->key = i; } t->next = x; cout << "The sequence of suicide is as follows : "; while (t != t->next) { for (int i = 1; i < M; i++) { t = t->next; } cout << t->next->key << " "; x = t->next; t->next = x->next; delete x; } cout << t->key << endl; }
■ 実行結果
> Enter the total number of people(N) : 9 <Enter>
> Who attempts suicide first?(M) : 5 <Enter>
> The sequence of suicide is as follows : 5 1 7 4 3 6 9 2 > 8
Referrer
1.227 sec
- 1: http://www.google.com/search?hl=en&q=%E3%83%AA%E3%83%B3%E3%82%AF%E3%83%AA%E3%82%B9%E3%83%88+%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0&btnG=Search
- 1: http://www.google.co.jp/search?hl=ja&rlz=1B3GGGL_jaJP248JP248&q=%E3%83%AA%E3%83%B3%E3%82%AF%E3%83%AA%E3%82%B9%E3%83%88+%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0&start=30&sa=N
- 1: http://www.google.co.jp/search?hl=ja&q=%E3%83%AA%E3%83%B3%E3%82%AF%E3%83%AA%E3%82%B9%E3%83%88%E3%80%80%E3%83%80%E3%83%96%E3%83%AB%E3%83%9D%E3%82%A4%E3%83%B3%E3%82%BF+next&lr=
- 1: http://www.google.co.jp/search?hl=ja&q=%E3%83%AA%E3%83%B3%E3%82%AF%E3%83%AA%E3%82%B9%E3%83%88%E3%80%80%E3%83%80%E3%83%96%E3%83%AB%E3%83%9D%E3%82%A4%E3%83%B3%E3%82%BF&lr=
- 1: http://www.google.co.jp/search?q=%E5%BE%AA%E7%92%B0%E3%83%AA%E3%82%B9%E3%83%88%E3%80%80%E3%82%B8%E3%83%A7%E3%82%BB%E3%83%95%E3%82%A1%E3%82%B9&lr=lang_ja&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:ja:official&client=firefox-a


