Guest User

Untitled

a guest
Aug 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace zgadywarka
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int guesses = 0;
  14.             int max = 100;
  15.             int guessMin = 0;
  16.             int guessMax = max / 2;
  17.  
  18.             Console.WriteLine($"Pomysl sobie liczbe od 1 do 100");
  19.  
  20.             while (guessMin != max)
  21.             {
  22.                 guesses++;
  23.                 Console.WriteLine($"Czy twoja liczba znajduje sie w przedziale od {guessMin} do {guessMax}?");
  24.                 string response = Console.ReadLine();
  25.  
  26.                 if (response?.ToLower().FirstOrDefault() == 't') {
  27.                     max = guessMax;
  28.                     guessMax -= (guessMax - guessMin) / 2;
  29.                 } else {
  30.                     guessMin = guessMax + 1;
  31.                     int difference = max - guessMax;
  32.                     guessMax += (int) Math.Ceiling(difference / 2f);
  33.                 }
  34.  
  35.                 if (guessMin + 1 == max) {
  36.                     guesses++;
  37.                     Console.WriteLine($"Czy twoja liczba to {guessMin}?");
  38.                     response = Console.ReadLine();
  39.                     if (response?.ToLower().FirstOrDefault() != 't')
  40.                         guessMin = max;
  41.                     }
  42.                     break;
  43.                 }
  44.             }
  45.             Console.WriteLine($"Twoja liczba to {guessMin}!");
  46.             Console.WriteLine($"Zostala odgadnieta w {guesses} ruchach");
  47.             Console.ReadLine();
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment