码迷,mamicode.com
首页 > 其他好文 > 详细

求上排的数在下排出现的次数

时间:2015-04-27 15:18:31      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:数组   算法   

技术分享题目:

      举一个例子:

      数值: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在下排出现了一次,。。。。。

     以此类推



#include <iostream>
using namespace std;
#define len 10
class NumberTB
{
private:
int top[len];
int bottom[len];
bool success;
public:
NumberTB();
int* getBottom();
void setNextBottom();
int getFrequency(int num);
};
NumberTB::NumberTB()
{
success = false;
//format top
for (int i = 0; i < len;i++)
{
top[i] = i;
}


}
int* NumberTB::getBottom()
{
int i = 0;
while (!success)
{
i++;
setNextBottom();
}
return bottom;
}
//set next bottom
void NumberTB::setNextBottom()
{
bool reB = true;
for (int i = 0; i < len; i++)
{
int frequecy = getFrequency(i);
if (bottom[i] != frequecy)
{
bottom[i] = frequecy;
reB = false;
}
}
success = reB;
}
//get frequency in bottom
int NumberTB::getFrequency(int num)//此处的num即指上排的数i
{
int count = 0;
for (int i = 0; i < len; i++)
{
if (bottom[i] == num)
count++;
}
return count;//count 即对应frequency
}


int main()
{
NumberTB nTB;
int* result = nTB.getBottom();
for (int i = 0; i < len; i++)
{
cout << *result++ << endl;
}
system("pause");
return 0;
}

求上排的数在下排出现的次数

标签:数组   算法   

原文地址:http://blog.csdn.net/wangfengfan1/article/details/45309637

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!