创新工厂面试题详解 我的代码

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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值