给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数
要求下排每个数都是先前上排那十个数在下排出现的次数。
上排的十个数如下:
【0,1,2,3,4,5,6,7,8,9】
举一个例子,
数值: 【 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在下排出现了1次,
3在下排出现了0次....
实现代码:
#include <iostream>
using namespace std;
const int LEN = 10;
int getfrequency(int b[],int n);
bool setBottom(int a[],int b[]);
int main()
{
int a[10] = {0,1,2,3,4,5,6,7,8,9};
int b[10] = {0};
while (!setBottom(a,b));
for (int i = 0; i < LEN; i++)
{
cout<<b[i]<<" ";
}
}
bool setBottom(int a[],int b[])
{
bool bStat = true;
int frequency;
for (int i = 0; i < LEN; i++)
{
frequency = getfrequency(b,a[i]);
if (frequency != b[i])
{
b[i] = frequency;
bStat = false;
}
}
return bStat;
}
int getfrequency(int b[],int n)
{
int count = 0;
for (int i = 0; i < LEN; i++)
{
if (b[i] == n)
{
count++;
}
}
return count;
}