fork download
  1.  
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<time.h>
  5. int check_heap(int a[], int n);
  6. void insert(int val, int a[], int *n);
  7. int deletemin(int a[],int *n);
  8.  
  9. int main(void) {
  10. int a[12] = {1,13,71,14,15,80,91,24,60,63};
  11. int n;
  12. int i,j;
  13. int b,c;
  14.  
  15. srandom(time(NULL));
  16.  
  17. b = random() % 100 + 1;
  18. n = 10;
  19. insert(b, a, &n);
  20.  
  21.  
  22. c = random() % 100 + 1;
  23. n = 11;
  24. insert(c, a, &n);
  25.  
  26. printf("%d\n", check_heap(a, 12));
  27.  
  28.  
  29. for(i = 0; i < 12; i++){
  30. printf("%3d",a[i]);
  31.  
  32. }
  33.  
  34.  
  35. deletemin(a, &n);
  36. for(j = 0; j < 11; j++){
  37.  
  38. printf("%3d",a[j]);
  39.  
  40. }
  41.  
  42. return 0;
  43. }
  44.  
  45. int check_heap(int a[], int n) {
  46. int i;
  47. int m;
  48. m = (n - 1) / 2;
  49. for(i = 0; i < m; i++)
  50. {
  51. if(a[i] >= a[i * 2 + 1])
  52. {
  53. return 0;
  54. }
  55. if(a[i] >= a[i * 2 + 2]){
  56. return 0;
  57. }
  58. }
  59.  
  60.  
  61. if(n % 2 == 0){
  62.  
  63. if(a[(n - 1)/2] > a[n - 1]){
  64.  
  65. return 0;
  66. }
  67. }
  68. return 1;
  69.  
  70. }
  71.  
  72. void insert(int val, int a[], int *n) {
  73.  
  74. int temp;
  75. int i;
  76.  
  77. a[*n] = val;
  78. i = *n;
  79.  
  80. while( 0 < i){
  81.  
  82. if(a[(i-1)/2] <= a[i])
  83. {
  84. break;
  85. }
  86.  
  87. temp = a[(i-1) / 2];
  88. a[(i-1) / 2] = a[i];
  89. a[i] = temp;
  90. i = (i - 1)/2;
  91.  
  92.  
  93. }
  94.  
  95. }
  96.  
  97. int deletemin(int a[],int *n){
  98.  
  99. int b;
  100. int temp;
  101. int i,j,k;
  102. i = 0;
  103. b = a[0];
  104. while(a[i] > 0)
  105. {
  106.  
  107. k = a[2*i+1] - a[2*i+2];
  108. if(k < 0){
  109. temp = a[2*i+1];
  110. a[2*i+1] = a[i];
  111. a[i] = temp;
  112. i = 2*i+1;
  113.  
  114.  
  115. }
  116. if(k > 0)
  117. {
  118.  
  119. temp = a[2*i+2];
  120. a[2*i+2] = a[i];
  121. a[i] = temp;
  122. i = 2*i+2;
  123.  
  124.  
  125. }
  126.  
  127.  
  128.  
  129. }
  130.  
  131.  
  132.  
  133. return b;
  134. }
  135.  
Time limit exceeded #stdin #stdout 5s 1676KB
stdin
Standard input is empty
stdout
Standard output is empty