您的位置:首页 > 其它

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

2012-05-15 16:19 357 查看
View Code

#include <iostream>
using namespace std;

const int SIZE=10;

int getFrequency(int index,int* bottom)
{
int count=0;
for(int i=0;i<SIZE;++i)
{
if(bottom[i]==index)
count++;
}
return count;
}

void Calc(const int* Upper,int* bottom)
{
bool isChange=true;
int loop_count=0;

while(isChange)
{
loop_count++;
isChange=false;
for(int i=0;i<SIZE;++i)
{
int fre=getFrequency(i,bottom);
if(bottom[i]!=fre)
{
bottom[i]=fre;
isChange=true;
}
}
}

for(int i=0;i<SIZE;++i)
cout <<bottom[i]<<" ";
cout <<endl;
cout <<"The number of loop is "<<loop_count<<endl;
}

int main()
{
int Upper[SIZE];
int Bottom[SIZE];
for(int i=0;i<SIZE;++i)
{
Upper[i]=i;
Bottom[i]=0;
}
Calc(Upper,Bottom);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐