Facebook
From Walloping Plover, 7 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 498
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. int losuj(int min, int max) {
  6.   int tmp;
  7.   if (max >= min)
  8.     max -= min;
  9.   else {
  10.     tmp = min - max;
  11.     min = max;
  12.     max = tmp;
  13.   }
  14.   return max ? (rand() % max + min) : min;
  15. }
  16.  
  17. int binarne(int tab[], int x, int n) {
  18.   int left = 0, right = n - 1;
  19.   int found = 0, m;
  20.   while(right - left >= 0 && found == 0) {
  21.           m = (left + right) / 2;
  22.           if(x == tab[m]) {
  23.                   found = 1;
  24.           } else {
  25.                   if(x < tab[m]) {
  26.                           right = m - 1;
  27.                   } else {
  28.                           left = m + 1;
  29.                   }
  30.           }
  31.   }
  32.   if(found == 1) {
  33.           return m;
  34.   } else {
  35.           return -1;
  36.   }
  37.  
  38. }
  39. int main() {
  40.   int tab[2000], j = 0;
  41.   for (j = 0; j < 2000; j++) {
  42.     tab[j] = j + 1;
  43.   }
  44.   int a = losuj(0, 2000);
  45.   printf("%d %d\n", a, binarne(tab, a, 2000));
  46.   return 0;
  47. }
  48.