/*
* FoxRabbit.cpp
*
* Created on: Oct 5, 2013
* Author: jiansong
*/
#include
using namespace std;
#include
#include
/*问题描述:
围绕着山顶有10个洞,一只狐狸和一只兔子住在各自的洞里。狐狸想吃掉兔子。一天,兔子对狐狸说:“你想吃我有一个条件,先把洞从1-10编上号,你从10号洞出发,先到1号洞找我;第二次隔1个洞找我,第三次隔2个洞找我,以后依次类推,次数不限,若能找到我,你就可以饱餐一顿。不过在没有找到我以前不能停下来。”狐狸满口答应,就开始找了。它从早到晚进了1000次洞,累得昏了过去,也没找到兔子,请问,兔子躲在几号洞里?
*/
void printSafeHole(unsigned int n, unsigned int times = 1000) {
unsigned int *array = new unsigned int[n];
if (array != NULL) {
memset(array, 0, sizeof(unsigned int) * n);
unsigned int current = 0;
array[0] = 1; // set init value
for (unsigned int steps=2; steps<=times; steps++) {
current = (steps + current) % n;
array[current] = 1;
}
// print unaccessed hole
cout << "Safe hole number: ";
for (unsigned int i=0; i
if (array[i] == 0) {
cout << i + 1 << " ";
}
}
cout << endl;
delete[] array;
}
else {
cout << "Error: Out of memory.";
}
}
#include "gtest/gtest.h"
TEST(FoxRabit, TenHoles) {
printSafeHole(10);
}
// Safe hole number: 2 4 7 9
TEST(FoxRabit, HunhdredHoles) {
printSafeHole(99, 10000);
}
阅读(276) | 评论(0) | 转发(0) |