> 文档中心 > C++ 堆模版

C++ 堆模版

请看完后点个赞,谢谢

//堆模版 22-03-27#include using namespace std;int heap[11]={0,1,1,2,5,4,4,3,7,6} ;int heapsz=0;void push(int n)//维护大顶堆{int now,next;//指向当前与下一个节点heapsz++;heap[heapsz]=n;now=heapsz;while (now>1){next=now/2;if (heap[now]>=heap[next]) swap(heap[now],heap[next]);else break;now=next;}}void print(){for (int i=1;i<=5;i++) cout<<heap[i]<<" ";}int main(){ios::sync_with_stdio(0);cin.tie(0);//建堆for (int i=1;i>tmp;push(tmp);print();}}

https://blog.csdn.net/zeekliu/article/details/124098799​​​​​​​ 

国际新闻