面试题一个

 1 /*对于 0 1 2 3 4 5 6 7 8 9
 2  *     _ _ _ _ _ _ _ _ _ _
 3  * 下划线上输入数字,使得下面数字代表上面数字出现的次数
 4  * 比如 1下面是3的话,表示1 在下面出现三次
 5  * 思路,因为有十个空格,也就是说下面出现十个数字,
 6  * 所以 (1)下面数字的和是10
 7  * 因为下面的数字的和是10   下面的数带表上面的数出现的次数
 8  * 所以 (2)上面的数乘以下面的数的和也是10
 9  * 由此  编程实现如下:        */
10 
11 #include <iostream>
12 using namespace std;
13 #define Len 10
14 
15 class NumberTB
16 {
17 private:
18     int top[Len];
19     int bottom[Len];
20     bool success;
21 public:
22     NumberTB();
23     int *getBottom();
24     void setNextBottom();
25     int getFrequency(int num);
26 };
27 
28 NumberTB::NumberTB()
29 {
30     success = false;
31     //format top
32     for(int i = 0; i < 10; i++)
33         top[i] = i;
34 
35 }
36 
37 int* NumberTB::getBottom()
38 {
39     int i = 0;
40     while(!success)
41     {
42         i++;
43         setNextBottom();
44     }
45     return bottom;
46 }
47 
48 //set next Bottom
49 void NumberTB::setNextBottom()
50 {
51     bool reB = true;
52     for(int i = 0; i < Len; i++)
53     {
54         int frequecy = getFrequency(i);
55         if(bottom[i] != frequecy)
56         {
57             bottom[i]= frequecy;
58             reB = false;
59         }
60     }
61     success = reB;
62 }
63 
64 //get Frequency in bottom
65 int NumberTB::getFrequency(int num) //num 代表上排数
66 {
67     int count = 0;
68     for(int i = 0; i < Len; i++)
69     {
70         if(bottom[i] == num)
71             count++;
72     }
73     return count;
74 }
75 
76 int main(void)
77 {
78     int i; 
79     NumberTB nTB;
80     int* result = nTB.getBottom();
81     cout << "原始数据: ";
82     for(i = 0; i < 10; i++)
83         cout << i << " ";
84     cout << endl;
85     cout << "出现次数: ";
86     for(i = 0; i < 10; i++)
87     {
88         cout << *result++ << " ";
89 
90     }
91     cout << endl;
92     return 0;
93 }
posted @ 2012-10-26 20:04  liangflying  阅读(171)  评论(0编辑  收藏  举报