[关闭]
@zhangche0526 2017-02-25T06:30:22.000000Z 字数 554 阅读 770

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. using namespace std;
  6. int heap[101];
  7. int heapSize;
  8. void put(int);
  9. void get();
  10. int main()
  11. {
  12. int x;cin>>x;
  13. put(x);
  14. return 0;
  15. }
  16. void put(int d)
  17. {
  18. int now,next;
  19. heap[++heapSize] = d;
  20. now = heapSize;
  21. while(now > 1)
  22. {
  23. next=now >> 1;
  24. if(heap[now] >= heap[next])break;
  25. swap(heap[now],heap[next]);
  26. now = next;
  27. }
  28. }
  29. void get()
  30. {
  31. int now,next,res;
  32. res = heap[1];
  33. heap[1]=heap[heapSize--];
  34. now = 1;
  35. while(now * 2 <= heapSize)
  36. {
  37. next = now * 2;
  38. if(next < heapSize && heap[next+1] < heap[next])next++;
  39. if(heap[now] <= heap[next]) break;
  40. swap(heap[now],heap[next]);
  41. now = next;
  42. }
  43. return res;
  44. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注