/* © MATTO MATTI 2017 http://mattomatti.com/pl/fs05 napisane przy użyciu Visual Studio Community 2015 2017-08-17 v 1.0 */ using System; namespace plcode1cs { class Program { static int[] utworzPrzesuniecia(int n) { int ile = (int)(Math.Ceiling(Math.Log(n, 2.0) + 0.5) + 1); int[] lista = new int[ile]; int potega = 1; for (int i = 0; i < ile; i++) { lista[i] = (n + potega) / (2 * potega); potega *= 2; } return lista; } static int wyszukiwanieJednolite(int[] a, int el, int[] przes) { int i = przes[0] - 1; int d = 0; while (true) { if (el == a[i]) { return i; } else if (przes[d] == 0) { return -1; } else { if (el < a[i]) { i -= przes[++d]; } else { i += przes[++d]; } } } } static void Main(string[] args) { Console.Write("Podaj ilość elementów na liście\nn = "); int n = Convert.ToInt32(Console.ReadLine()); int[] a = new int[n]; Console.Write("Podaj elementy listy posortowane rosnąco:\n"); for (int i = 0; i < n; ++i) a[i] = Convert.ToInt32(Console.ReadLine()); int[] przes = utworzPrzesuniecia(n); for (int i = 0; i < 20; ++i) { int poz = wyszukiwanieJednolite(a, i, przes); Console.Write("Element {0} ", i); if (poz == -1) { Console.WriteLine("nie występuje w tablicy"); } else { Console.WriteLine("jest na pozycji i = {0}", poz); } } Console.ReadKey(); } } }