/
SamMih
/
Algorithms_HOMEWORK1
Обзор
Документация
Войти
/
SamMih
/
Algorithms_HOMEWORK1
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Main.java
38 строк
1 KB
SamMih
Algorithms_HOMEWORK1
18 июл 2025, 10:35
18 июл 2025, 10:35
827fdf0
Код
Авторство
О чём код?
public class Main { public static void main(String[] args) { int[] prices = {13, 17, 19, 25, 25, 25, 25, 25, 25, 27, 30}; System.out.println("Для 31: " + countMore(prices, 31)); // 0 System.out.println("Для 26: " + countMore(prices, 26)); // 2 System.out.println("Для 25: " + countMore(prices, 25)); // 2 System.out.println("Для 20: " + countMore(prices, 20)); // 8 } public static int countMore(int[] prices, int money) { if (prices[0] > money) { return prices.length; } if (prices[prices.length - 1] < money) { return 0; } int left = 0; int right = prices.length - 1; int count = 0; while (left < right) { int middle = (left + right) / 2; if (prices[middle] > money) { count = middle; right = middle - 1; } else { left = middle + 1; } } return prices.length - count; } }