优先级队列--细胞分裂模拟

本文介绍了一个使用C++实现的细胞分裂模拟器,该模拟器能够模拟细胞在特定时间范围内随机分裂的过程,通过优先级队列跟踪每个细胞的分裂时间,展示了细胞数量随时间增长的变化。

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

模拟一种细胞在诞生(即上次分裂)后会在500到2000秒内分裂为两个细胞,每个细胞幽暗照同样的规律继续分裂。

#include <queue>
#include<ctime>
using namespace std;
const int SPLIT_TIME_MIN = 500;
const int SPLIT_TIME_MAX = 2000;
class Cell;
priority_queue<Cell> cellQueue;
class Cell {
private:
	static int count;	//细胞总数
	int id;		//当前细胞编号
	int time;	//细胞分裂时间
public:
	Cell(int birth) :id(count++) {	//birth为细胞诞生时间
		//初始化,确定细胞分裂时间
		time = birth + (rand() % (SPLIT_TIME_MAX - SPLIT_TIME_MIN) + SPLIT_TIME_MIN);
	}
	int getId()const { return id; }
	int getSplitTime()const { return time; }
	bool operator <(const Cell & s) const {
		return time > s.time;
	}
	void split()const {
		Cell child1(time), child2(time);	//建立两个子细胞
		cout << time << "s:Cell#" << id << "splits to#"
			<< child1.getId() << "and #" << child2.getId() << endl;
		cellQueue.push(child1);
		cellQueue.push(child2);
	}
};
int Cell::count = 0;
int main() {
	srand(static_cast<unsigned>(time(0)));
	int t;								//模拟时间长度
	cout << "Sinulaton time:";
	while (cin >> t) {
		cellQueue.push(Cell(0));			//将第一个细胞压入优先级队列
		while (cellQueue.top().getSplitTime() <= t) {
			cellQueue.top().split();			//模拟下一个细胞分裂
			cellQueue.pop();				//将刚分裂的细胞弹出
		}
	}
	return 0;
}

运行效果
运行效果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值