Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- //egy elem megkeresése rendezett tömbben(logaritmikus keresés)
- int[] szamok = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
- int keresett, i, j;
- Console.Write("\nMi a keresett szám?: ");
- keresett = Convert.ToInt32(Console.ReadLine());
- i= 1;
- j=10;
- int k=0;
- bool talalt=false;
- while (talalt == false && i <= j && j<10)
- {
- k = (i + j) / 2;
- if(szamok[k]==keresett)
- {
- talalt=true;
- }
- else if(szamok[k]<keresett)
- i=k+1;
- else if(szamok[k]>keresett)
- {
- j=k-1;
- }
- }
- if (talalt == true)
- {
- Console.Write("\nMegvan a szám! Helye: " + (k+1));
- }
- else
- {
- Console.Write("\nNincs meg a szám!" + k);
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement