您的位置:首页 > 其它

2017-01-21 20:27 387 查看

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;

int heap[101];
int heapSize;
void put(int);
void get();
int main()
{
int x;cin>>x;
put(x);
return 0;
}
void put(int d)
{
int now,next;
heap[++heapSize] = d;
now = heapSize;
while(now > 1)
{
next=now >> 1;
if(heap[now] >= heap[next])break;
swap(heap[now],heap[next]);
now = next;
}
}
void get()
{
int now,next,res;
res = heap[1];
heap[1]=heap[heapSize--];
now = 1;
while(now * 2 <= heapSize)
{
next = now * 2;
if(next < heapSize && heap[next+1] < heap[next])next++;
if(heap[now] <= heap[next]) break;
swap(heap[now],heap[next]);
now = next;
}
return res;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: