Chinaunix首页 | 论坛 | 博客
  • 博客访问: 109051
  • 博文数量: 20
  • 博客积分: 1910
  • 博客等级: 上尉
  • 技术积分: 485
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-10 15:46
文章分类

全部博文(20)

文章存档

2013年(3)

2012年(4)

2011年(10)

2010年(1)

2009年(2)

我的朋友

分类: C/C++

2013-10-07 09:20:20

/*
 * 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) |
0

上一篇:Reverse a LinkedList

下一篇:没有了

给主人留下些什么吧!~~