给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数
要求下排每个数都是先前上排那十个数在下排出现的次数。
上排的十个数如下:
【0,1,2,3,4,5,6,7,8,9】
#include<iostream>
using namespace std;#define len 20
class numberTB {
private:
int top[len];
int bottom[len];
bool success;
int firstNumber;
public:
numberTB(int firstnumber);
int* getBottom();
void setnextBottom();
int getFrequency(int num);
};
numberTB::numberTB(int firstnumber) {
firstNumber = firstnumber;
success = false;
for (int i = 0; i < len; i++) {
top[i] = i+firstNumber;
}
}
int* numberTB::getBottom() {
int i = 0;
while (!success) {
i++;
setnextBottom();
}
return bottom;
}
void numberTB::setnextBottom() {
bool reB = true;
for (int i = 0; i < len; i++) {
int frequency = getFrequency(top[i]);
if (bottom[i] != frequency) {
bottom[i] = frequency;
reB = false;
}
success = reB;
}
}
int numberTB::getFrequency(int num) {
int count = 0;
for (int i = 0; i < len; i++) {
if (bottom[i] == num) count++;
}
return count;
}
int main() {
int firstnumber = 10;
numberTB numbertb(firstnumber);
int* result = numbertb.getBottom();
for (int i = 0; i < len; i++) {
cout << *result++ << endl;
}
return 0;
}
题目相当绕,要是面试问这种题我肯定呆十分钟什么也想不出来吧。。。过程就是不断回溯检测,当然也是看了答案才懂的。。