SHOW:
|
|
- or go back to the newest paste.
1 | using System; | |
2 | ||
3 | class BinSearchLargerstNumber | |
4 | { | |
5 | static void Main() | |
6 | { | |
7 | Console.WriteLine("How many numbers you want to enter?"); | |
8 | int n = int.Parse(Console.ReadLine()); | |
9 | int[] numbers = new int[n]; | |
10 | ||
11 | Console.WriteLine("Start entering numbers:"); | |
12 | for (int i = 0; i < numbers.Length; i++) | |
13 | { | |
14 | numbers[i] = int.Parse(Console.ReadLine()); | |
15 | } | |
16 | ||
17 | Console.WriteLine("Please enter K:"); | |
18 | int k = int.Parse(Console.ReadLine()); | |
19 | ||
20 | ||
21 | Array.Sort(numbers); | |
22 | int index = Array.BinarySearch(numbers, k); | |
23 | ||
24 | if (numbers[0] > k) | |
25 | { | |
26 | - | Console.WriteLine("There isn't a such a number in the array."); |
26 | + | Console.WriteLine("There isn't such a number in the array."); |
27 | } | |
28 | else | |
29 | { | |
30 | if (index >= 0) | |
31 | { | |
32 | Console.WriteLine("The largest number in the array which is less or equal to K is {0}", numbers[index]); | |
33 | } | |
34 | else | |
35 | { | |
36 | Console.WriteLine("The largest number in the array which is less or equal to K is {0}", numbers[-index - 2]); | |
37 | } | |
38 | } | |
39 | } | |
40 | } |