题目:
举一个例子:
数值: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 10class 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 topfor (int i = 0; i < len;i++){top[i] = i;}}int* NumberTB::getBottom(){int i = 0;while (!success){i++;setNextBottom();}return bottom;}//set next bottomvoid 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 bottomint 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;}
打掉的应是脆弱的铁屑,锻成的将是锋利的钢刀。