求上排的数在下排出现的次数

本文介绍了一个用于统计数字在分配列表中出现频率的算法,包括初始化数组、获取底部数组、设置下一个底部数组以及计算特定数字的频率。通过实例演示了如何实现这一算法并输出每个数字在列表中的出现次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

惊恐题目:

      举一个例子:

      数值: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在下排出现了一次,。。。。。

     以此类推



#include <iostream>
using namespace std;
#define len 10
class NumberTB
{
private:
int top[len];
int bottom[len];
bool success;
public:
NumberTB();
int* getBottom();
void setNextBottom();
int getFrequency(int num);
};
NumberTB::NumberTB()
{
success = false;
//format top
for (int i = 0; i < len;i++)
{
top[i] = i;
}


}
int* NumberTB::getBottom()
{
int i = 0;
while (!success)
{
i++;
setNextBottom();
}
return bottom;
}
//set next bottom
void NumberTB::setNextBottom()
{
bool reB = true;
for (int i = 0; i < len; i++)
{
int frequecy = getFrequency(i);
if (bottom[i] != frequecy)
{
bottom[i] = frequecy;
reB = false;
}
}
success = reB;
}
//get frequency in bottom
int NumberTB::getFrequency(int num)//此处的num即指上排的数i
{
int count = 0;
for (int i = 0; i < len; i++)
{
if (bottom[i] == num)
count++;
}
return count;//count 即对应frequency
}


int main()
{
NumberTB nTB;
int* result = nTB.getBottom();
for (int i = 0; i < len; i++)
{
cout << *result++ << endl;
}
system("pause");
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值