This commit is contained in:
@@ -14,4 +14,4 @@ jobs:
|
|||||||
javac -d out $(find java common -name "*.java")
|
javac -d out $(find java common -name "*.java")
|
||||||
- name: Run Binary Search tests
|
- name: Run Binary Search tests
|
||||||
run: |
|
run: |
|
||||||
java -ea -cp out search.BinarySearchTest Base
|
java -ea -cp out search.BinarySearchTest Base 3637
|
||||||
|
|||||||
@@ -10,6 +10,12 @@
|
|||||||
* Класс `BinarySearch` должен находиться в пакете `search`
|
* Класс `BinarySearch` должен находиться в пакете `search`
|
||||||
* [Исходный код тестов](java/search/BinarySearchTest.java)
|
* [Исходный код тестов](java/search/BinarySearchTest.java)
|
||||||
* [Откомпилированные тесты](artifacts/search/BinarySearchTest.jar)
|
* [Откомпилированные тесты](artifacts/search/BinarySearchTest.jar)
|
||||||
|
* *3637*
|
||||||
|
* На вход подаётся число `x` и массив, отсортированный по невозрастанию.
|
||||||
|
* Требуется вывести число элементов массива, равных `x`.
|
||||||
|
* Не допускается использование типов `long` и `BigInteger`.
|
||||||
|
* Класс должен иметь имя `BinarySearch3637`
|
||||||
|
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
## Домашнее задание 1. Обработка ошибок
|
## Домашнее задание 1. Обработка ошибок
|
||||||
|
|||||||
Binary file not shown.
88
java/search/BinarySearch3637.java
Normal file
88
java/search/BinarySearch3637.java
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package search;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Nikita Doschennikov (me@fymio.us)
|
||||||
|
*/
|
||||||
|
public class BinarySearch3637 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
IntList a = new IntList();
|
||||||
|
int x = Integer.parseInt(args[0]);
|
||||||
|
|
||||||
|
int n = args.length;
|
||||||
|
|
||||||
|
for (int i = 1; i < n; i++) {
|
||||||
|
a.put(Integer.parseInt(args[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
searchIterativeIncreasing(x, a.getReversed()) -
|
||||||
|
searchIterativeDecreasing(x, a)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int searchIterativeIncreasing(int x, IntList a) {
|
||||||
|
if (a.getLength() == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int low = 0,
|
||||||
|
high = a.getLength() - 1;
|
||||||
|
|
||||||
|
while (low <= high) {
|
||||||
|
int mid = low + (high - low) / 2;
|
||||||
|
|
||||||
|
if (a.get(mid) <= x) {
|
||||||
|
low = mid + 1;
|
||||||
|
} else {
|
||||||
|
high = mid - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return low;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int searchIterativeDecreasing(int x, IntList a) {
|
||||||
|
if (a.getLength() == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int low = 0,
|
||||||
|
high = a.getLength() - 1;
|
||||||
|
|
||||||
|
while (low <= high) {
|
||||||
|
int mid = low + (high - low) / 2;
|
||||||
|
|
||||||
|
if (a.get(mid) <= x) {
|
||||||
|
high = mid - 1;
|
||||||
|
} else {
|
||||||
|
low = mid + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return low;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int searchRecursive(int x, IntList a) {
|
||||||
|
return searchRecursiveHelper(x, a, 0, a.getLength() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int searchRecursiveHelper(
|
||||||
|
int x,
|
||||||
|
IntList a,
|
||||||
|
int low,
|
||||||
|
int high
|
||||||
|
) {
|
||||||
|
if (low > high) {
|
||||||
|
return low;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mid = low + (high - low) / 2;
|
||||||
|
|
||||||
|
if (a.get(mid) <= x) {
|
||||||
|
return searchRecursiveHelper(x, a, low, mid - 1);
|
||||||
|
} else {
|
||||||
|
return searchRecursiveHelper(x, a, mid + 1, high);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,8 @@ import java.util.function.Function;
|
|||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static java.util.stream.IntStream.range;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
|
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
|
||||||
*/
|
*/
|
||||||
@@ -28,10 +30,18 @@ public final class BinarySearchTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// === 3637
|
||||||
|
|
||||||
|
private static int count(final int c, final int x, final int[] a) {
|
||||||
|
return (int) range(0, a.length).filter(i -> a[i] == x).count();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// === Common code
|
// === Common code
|
||||||
|
|
||||||
public static final Selector SELECTOR = new Selector(BinarySearchTest.class)
|
public static final Selector SELECTOR = new Selector(BinarySearchTest.class)
|
||||||
.variant("Base", Solver.variant0("", Kind.DESC, BinarySearchTest::base))
|
.variant("Base", Solver.variant0("", Kind.DESC, BinarySearchTest::base))
|
||||||
|
.variant("3637", Solver.variant0("3637", Kind.DESC, BinarySearchTest::count))
|
||||||
;
|
;
|
||||||
|
|
||||||
public static void main(final String... args) {
|
public static void main(final String... args) {
|
||||||
|
|||||||
@@ -12,6 +12,10 @@ public class IntList {
|
|||||||
|
|
||||||
public IntList() {}
|
public IntList() {}
|
||||||
|
|
||||||
|
public IntList(int[] list) {
|
||||||
|
this.list = list;
|
||||||
|
}
|
||||||
|
|
||||||
public void put(int val) {
|
public void put(int val) {
|
||||||
if (idx >= list.length) {
|
if (idx >= list.length) {
|
||||||
list = Arrays.copyOf(list, list.length * 2);
|
list = Arrays.copyOf(list, list.length * 2);
|
||||||
@@ -24,6 +28,16 @@ public class IntList {
|
|||||||
return idx;
|
return idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IntList getReversed() {
|
||||||
|
IntList reverse = new IntList(new int[idx]);
|
||||||
|
|
||||||
|
for (int i = idx - 1; i >= 0; i--) {
|
||||||
|
reverse.put(list[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return reverse;
|
||||||
|
}
|
||||||
|
|
||||||
public int get(int index) {
|
public int get(int index) {
|
||||||
return list[index];
|
return list[index];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user