13個人中找出叛徒的問題(耶穌問題)經典做法,叛徒耶穌
//耶穌有13個門徒,其中有一個就是出賣耶穌的叛徒,請用排除法找出這位叛徒:13人圍坐一圈,從第一個開始報號:1,2,3,1,2,3...。凡是報到“3”就退出圈子,最後留在圈子內的人就是出賣耶穌的叛徒。請找出它原來的序號
// int a[13] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
// int number = 13, i = 0, count = 0;//number用來記錄剩下大不是0的數,count值為1, 2, 3
// while (number > 1) {
// if (a[i] != 0) {
// count ++;
// }
// if (count == 3) {
// a[i] = 0;
// count = 0;
// number --;
// }
// i ++;
// if (i == 13) {
// i = 0;
// }
// }
// for ( i = 0; i < 13; i++) {
// if (a[i] > 0) {
// printf("%d", a[i]);
// }
// }
編程題:耶穌有13個門徒,其中有一個就是出賣耶穌的叛徒,用排除法找出這位叛徒:13人圍坐一圈,從第一
這個排版實在是沒人會幫你看。
耶穌有13個門徒,其中有一個就是出賣耶穌的叛徒,用排除法找出這位叛徒:
丟手絹演算法
/////////////////////////////////////////
// 魔比斯環演算法 /
// n個人,從第k個人開始數,數到a的離開 /
// 問你最後一個離開的是誰,或者所有的 /
// 人離開順序 /
/////////////////////////////////////////
#include <iostream>
using namespace std;
class Moebius {
private:
int* place;
int n; //圈內人數
int k; //從第k個人報數
int a; //報數的終止值(從1 - a報數)
void delPlace(int); //報數為a的人離開環
public:
Moebius (){}; //無參建構函式
Moebius ( int, int, int );//建構函式
void Moebius::listInit (); // 得到環的原始順序列表
void Moebius::listMove ();//得到環的出圈順序
};
Moebius::Moebius ( int _n, int _k, int _a ) { // 定義建構函式
int i;
n = _n;
k = _k;
a = _a;
place = new int[n];
for ( i = 0; i < n; i++ )
place[i] = i + 1;
}
void Moebius::listInit () { // 輸出環內人員原始順序列表
int i;
cout << "魔比斯環初始人員順序列表:" << endl;
for ( i = 0; i < n; i++ )
cout << " " << place[i];
cout << endl;
}
void Moebius::listMove () { //輸出環內人員離開順序
int iPlace = k - 1; //確定第一個報數的人的位置
cout << "魔比斯環內的人離開順序列表:" << endl;
while ( n ) { //直到環內沒人為止
iPlace = (iPlace ......餘下全文>>