Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class OdgadywanieLiczbyDZ
- {
- private static int liczba = new Random().nextInt(1000+1);
- private static int krok;
- public static void main(String[] args)
- {
- znajdz(0,1001);
- }
- static void znajdz(int a, int b)
- {
- krok++;
- int polowa = a + (b-a)/2;
- if (polowa == liczba)
- System.out.println("Liczba " + liczba + " znaleziona po " + krok + " krokach.\n");
- else
- {
- if (liczba < polowa) znajdz(a, polowa);
- if (polowa < liczba) znajdz(polowa, b);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement