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

/*
腾讯面试题:
给你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 次....
以此类推..

此程序采用穷举法
*/


#include <iostream>
using namespace std;
#define  len 100
class TBNumber
{
public:
	TBNumber()
	{
		for (int i=0;i<len;i++)
		{
			top[i]=i;
			bottom[i]=0;
		}
		success=false;
	}
	void  setBottom();
	void adjustBottom();
	int getFrequency (int i);
	
private:
	int top[len];
	int bottom[len];
	bool success;
};

void TBNumber::setBottom()
{

	while(!success)
	{
		adjustBottom();	
	}

// 	for (int i=0;i<len;i++)
// 	{
// 		cout<<top[i]<<"\t";
// 	}

	cout<<endl;

	for (int i=0;i<len;i++)
	{
		cout<<bottom[i]<<"\t";
	}
}

void TBNumber::adjustBottom()
{
	bool ret=true;//标志,判断bottom中所有下标是否已经符合条件
	for (int i=0;i<len;i++)
	{
		if (bottom[i]!=getFrequency(top[i]))
		{
			ret=false;
			bottom[i]=getFrequency(i);
		}
	}
	success=ret;
}
int TBNumber::getFrequency(int i)
{
	int frequency=0;
	for (int j=0;j<len;j++)//获取bottom中i出现的频率
	{
		if (bottom[j]==i)
		{
			frequency++;
		}
	}
	return frequency;

}

int main()
{
	TBNumber tbNumber;
	tbNumber.setBottom();

}

  

posted @ 2011-10-19 10:45  refazy  阅读(1292)  评论(0编辑  收藏  举报