您的位置:首页 > 其它

求下排每个数都是先前上排那十个数在下排出现的次数

2015-08-25 21:07 429 查看
http://blog.csdn.net/heaven13483/article/details/7925621

给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数

要求下排每个数都是先前上排那十个数在下排出现的次数。

上排的十个数如下:【0,1,2,3,4,5,6,7,8,9】

举个例子,

上排数值: 0,1,2,3,4,5,6,7,8,9

下排数值: 6,2,1,0,0,0,1,0,0,0

0在下排出现了6次,1在下排出现了2次,

2在下排出现了1次,3在下排出现了0次....

思路:

它的原型跟八皇后有点类似,都是用回溯递归的方法去一次一次尝试,直到找出正确解。

具体的想法是:不断的去从下排数组中捉取在上排数组中对应位置中出现的个数,如果个数不对就更新下排数组中对应的值,只到找到正确值。(下排数组先初始为任意值)

如:

上排数组A:0,1,2,3,4,5,6,7,8,9

下排数组B:0,1,2,3,4,5,6,7,8,9

从上牌数组Index = 0开始,A[0] = 0,0在下排数组中的个数为1,那么下排数组B此时就要更新为:1,1,2,3,4,5,6,7,8,9,

Index = 1, A[1] = 1, 1在下排数组中的个数为2,那么下排数组B此时就要更新为:1,2,2,3,4,5,6,7,8,9,从此不断的往下进行,只要找不到正确值就一直往下进行,如果Index >= 数组长度时,那么重新恢复Index = 0再往下进行测试直到找出正确解。
但这好象只能解决如上所述的情况,,即连续的N个数。。

[cpp]
view plaincopyprint?

#include <iostream>
using namespace std;
#define MAX_LEN 10

class C_NumberTB
{
private:
int m_aryTop[MAX_LEN];
int m_aryBottom[MAX_LEN];
bool m_success;

public:
C_NumberTB();

public:
int* GetBottom();
void SetNextBottom();
int GetFrequecy(int nValue);
};

C_NumberTB::C_NumberTB()
{
m_success = false;

//format top
for(int i = 0; i < MAX_LEN; i++)
{
m_aryTop[i] = i;
m_aryBottom[i] = i;
}
}

int* C_NumberTB::GetBottom()
{
int i = 0;
while(!m_success)
{
i++;
SetNextBottom();
}

return m_aryBottom;
}

//set next bottom
void C_NumberTB::SetNextBottom()
{
bool bRet = true;

for(int i = 0; i < MAX_LEN; i++)
{
int nFreq = GetFrequecy(i);
if(m_aryBottom[i] != nFreq)
{
m_aryBottom[i] = nFreq;
bRet = false;
}
}

m_success = bRet;
}


//get frequency in bottom
int C_NumberTB::GetFrequecy(int nValue) //此处的nValue 即指上排的数i

{
int nCnt = 0;
for(int i = 0; i < MAX_LEN; i++)
{
if(m_aryBottom[i] == nValue)
nCnt++;
}

return nCnt; //nCnt 即对应nFreq

}



int main()
{
C_NumberTB objTB;
int* pResult = objTB.GetBottom();

for(int i= 0 ;i < MAX_LEN; i++)
{
cout << *pResult++ << endl;
}

return 0;
}

#include <iostream>
using namespace std;
#define		MAX_LEN	10

class C_NumberTB
{
private:
	int m_aryTop[MAX_LEN];
	int m_aryBottom[MAX_LEN];
	bool m_success;

public:
	C_NumberTB();

public:
	int* GetBottom();
	void SetNextBottom();
	int GetFrequecy(int nValue);
};

C_NumberTB::C_NumberTB()
{
	m_success = false;

	//format top
	for(int i = 0; i < MAX_LEN; i++)
	{
		m_aryTop[i] = i;
		m_aryBottom[i] = i;
	}
}

int* C_NumberTB::GetBottom()
{
	int i = 0;
	while(!m_success)
	{
		i++;
		SetNextBottom();
	}

	return m_aryBottom;
}

//set next bottom
void C_NumberTB::SetNextBottom()
{
	bool bRet = true;

	for(int i = 0; i < MAX_LEN; i++)
	{
		int nFreq = GetFrequecy(i);
		if(m_aryBottom[i] != nFreq)
		{
			m_aryBottom[i] = nFreq;
			bRet = false;
		}
	}

	m_success = bRet;
}

//get frequency in bottom
int C_NumberTB::GetFrequecy(int nValue) //此处的nValue 即指上排的数i
{
	int nCnt = 0;
	for(int  i = 0; i < MAX_LEN; i++)
	{
		if(m_aryBottom[i] == nValue)
			nCnt++;
	}

	return nCnt; //nCnt 即对应nFreq
}

int main()
{
	C_NumberTB objTB;
	int* pResult = objTB.GetBottom();

	for(int  i= 0 ;i < MAX_LEN; i++)
	{
		cout << *pResult++ << endl;
	}

	return 0;
}


在网上搜到的一篇更为高深的解题方法,,纯分析而得到的,好象更为精确。。

解题思路:关键是理解“要求下排每个数都是先前上排那十个数在下排出现的次数”。
做以下分析:设总共有n个数,上排a[0...n-1],下排b[0...n-1],。
1)下排n个数的累加和为n,即b[0]+b[1]+...+b[n-1] = n
2)ai*bi的累加和也为n,即a[0]*b[0]+a[1]*b[1]+...+a[n-1]*b[n-1] = n
3)对于b中任意一个元素b[j], 都存在i,a[i] = b[j].
4)对于b中任意一个元素b[j],都有b[j] >= 0
5)如果a中存在负数。其在b中出现的次数一定为0. 如果a中数值大于n,则其出现次数也为0.
6)a中至少有两个非0数值在b中出现的次数非0
a:由1)n > n*b[i],其中b[i]为最小值,则a b中一定均有数值0,否则无解。设a[0] = 0,b[0]为a[0]在b中出现次数。
b:由于b中一定存在0,则0的出现次数一定大于0,因此b[0]>0 且b[0] < n,b[1...n-1]中至少一个值为0. 非0元素出现的次数一共是n-b[0].
c:有2)和6)对任意a[i],a[i]*b[i] < n,即b[i] < n/a[i],对所有a[i]>=n/2的元素中,在b中出现的次数必须最多只有1个出现次数不为0,且为1.其余出现次数均为0,即[1, n/2)范围内最多只有n/2-1个元素,故0出现的次数必不小于n/2, [n/2,n)范围内的元素必有一个出现次数为1。因此a数列中也必须有1,否则无解。
d:有c得在数值范围为(0,n/2)中(假设有x这样的数)出现的次数和s为n - b[0]或n-b[0]-1。其中1出现的次数至少为1(由c得)。又如果1出现的次数为1,则1出现的次数已经为2,故1出现的次数必大于1.设为x,则x出现的次数至少为1,而x>1,如果x出现的次数大于1,那么必须要有其他数出现的次数为x,这样无法收敛。故x出现的次数只能为1,1出现的次数只能为2.
结论:
0出现的次数为n-4,1出现的次数为2.2出现的次数为1。n-4出现的次数为1.如果数列中无这四个数,则无解。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: