创新工厂面试题详解 我的代码
2011-09-23 01:05
246 查看
原贴 http://blog.csdn.net/nokiaguy/article/details/6800209#cpp
运行结果:
代码:
运行结果:
1 people caught 2 fishes People[00] took 1 fishes within total 2 fishes 2 people caught 7 fishes People[00] took 3 fishes within total 7 fishes People[01] took 1 fishes within total 3 fishes 3 people caught 25 fishes People[00] took 8 fishes within total 25 fishes People[01] took 5 fishes within total 16 fishes People[02] took 3 fishes within total 10 fishes 4 people caught 253 fishes People[00] took 63 fishes within total 253 fishes People[01] took 47 fishes within total 189 fishes People[02] took 35 fishes within total 141 fishes People[03] took 26 fishes within total 105 fishes 5 people caught 3121 fishes People[00] took 624 fishes within total 3121 fishes People[01] took 499 fishes within total 2496 fishes People[02] took 399 fishes within total 1996 fishes People[03] took 319 fishes within total 1596 fishes People[04] took 255 fishes within total 1276 fishes 6 people caught 46651 fishes People[00] took 7775 fishes within total 46651 fishes People[01] took 6479 fishes within total 38875 fishes People[02] took 5399 fishes within total 32395 fishes People[03] took 4499 fishes within total 26995 fishes People[04] took 3749 fishes within total 22495 fishes People[05] took 3124 fishes within total 18745 fishes 7 people caught 823537 fishes People[00] took 117648 fishes within total 823537 fishes People[01] took 100841 fishes within total 705888 fishes People[02] took 86435 fishes within total 605046 fishes People[03] took 74087 fishes within total 518610 fishes People[04] took 63503 fishes within total 444522 fishes People[05] took 54431 fishes within total 381018 fishes People[06] took 46655 fishes within total 326586 fishes Press any key to continue . . .
代码:
// Fish.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <assert.h> #include <vector> using std::vector; static bool IsExpected(int Fish, int People) { if (Fish <= 1) { return false; } return ((Fish - 1) % People == 0); } //-1 means fish count error static int GetNextFish(int Fish, int People) { assert(IsExpected(Fish, People)); int NextFish = (People - 1) * (Fish - 1) / People; return (IsExpected(NextFish, People)? NextFish : -1); } static int GetAnValiadFish(int kk, int People) { return (People * kk + 1); } static bool TestValidFishCount(int Fish, int People, vector<int>& vecFish) { vecFish.clear(); if (!IsExpected(Fish, People)) { return false; } vecFish.push_back(Fish); bool bret = true; for (int ii=1; ii<People; ++ii) { Fish = GetNextFish(Fish, People); if (Fish < 0) { bret = false; break; } vecFish.push_back(Fish); } return bret; } static void PrintResults(int People, int Fish, const vector<int>& vecFish) { _ftprintf_s(stdout, L"%d people caught %d fishes" L"\r\n", People, Fish); for (size_t ii=0; ii<vecFish.size(); ++ii) { _ftprintf_s(stdout, L"\t" L"People[%02d] " L"took %d fishes within total %d fishes" L"\r\n", ii, (vecFish[ii]-1)/People, vecFish[ii]); } _ftprintf_s(stdout, L"\r\n\r\n"); } static int GetTotalFish(int People, vector<int>& vecFish) { int kk = 1; int Fish = 0; for (;;) { Fish = GetAnValiadFish(kk, People); if (TestValidFishCount(Fish, People, vecFish)) { break; } ++kk; } return Fish; } int _tmain(int argc, _TCHAR* argv[]) { for (int People = 1; People < 8; ++People) { vector<int> vecFish; int Fish = GetTotalFish(People, vecFish); PrintResults(People, Fish, vecFish); } return 0; }
相关文章推荐
- 创新工厂面试题详解:共打了多少鱼 -- 正解 递归求解法
- java面试题:string,stringbuffer详解(通过代码说明)
- 创新工厂面试题详解:共打了多少鱼
- 创新工厂面试题详解:共打了多少鱼
- 创新工厂面试题详解:共打了多少鱼
- 创新工厂面试题详解:共打了多少鱼
- java面试题之try中含return语句时代码的执行顺序详解
- 创新工厂面试题详解:共打了多少鱼
- java面试题之try中含return语句时代码的执行顺序详解
- 创新工厂面试题详解:共打了多少鱼
- java面试题丢手帕问题 代码详解-- 跟着韩老师学JAVA!!!
- 也谈 创新工厂面试题详解:共打了多少鱼
- 知乎上小米变相约瑟夫环面试题微软解法的golang代码
- stl map底层之红黑树插入步骤详解与代码实现
- C++ 面试题翻译电话号码实例代码
- AngularJS过滤器详解及示例代码
- WindowsError的错误代码详解
- Kettle5.3源代码编译详解
- AngularJs bootstrap详解及示例代码
- CIFAR10 代码分析详解——cifar10.py