原贴 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;
}