[算法练习]根据上排给出十个数,在其下排填出对应的十个数

View Code
 1 #include <iostream>
 2 using namespace std;
 3 
 4 const int SIZE=10;
 5 
 6 int getFrequency(int index,int* bottom)
 7 {
 8     int count=0;
 9     for(int i=0;i<SIZE;++i)
10     {
11         if(bottom[i]==index)
12             count++;
13     }
14     return count;
15 }
16 
17 void Calc(const int* Upper,int* bottom)
18 {
19     bool isChange=true;
20     int loop_count=0;
21 
22     while(isChange)
23     {
24         loop_count++;
25         isChange=false;
26         for(int i=0;i<SIZE;++i)
27         {
28             int fre=getFrequency(i,bottom);
29             if(bottom[i]!=fre)
30             {
31                 bottom[i]=fre;
32                 isChange=true;
33             }
34         }
35     }
36 
37     for(int i=0;i<SIZE;++i)
38         cout <<bottom[i]<<" ";
39     cout <<endl;
40     cout <<"The number of loop is "<<loop_count<<endl;
41 }
42 
43 int main()
44 {
45     int Upper[SIZE];
46     int Bottom[SIZE];
47     for(int i=0;i<SIZE;++i)
48     {
49         Upper[i]=i;
50         Bottom[i]=0;
51     }
52     Calc(Upper,Bottom);
53     return 0;
54 }

 

posted @ 2012-05-15 16:19  Cavia  阅读(267)  评论(0编辑  收藏  举报