Skip to content

Commit

Permalink
2 yes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andeo1812 committed Apr 6, 2022
1 parent 1a628cc commit 22032f6
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 28 deletions.
8 changes: 8 additions & 0 deletions CPPLINT.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
headers=h
linelength=120
filter=-whitespace/tab
filter=-runtime/int
filter=-legal/copyright
filter=-build/include_subdir
filter=-build/include
filter=-readability/casting
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

all: clear build launch

TARGET = 1
TARGET = 2

TARGET_MODULE = module_1

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

1 - 4 - YES

2 - 4
2 - 4 - YES

3 - 2

Expand Down
105 changes: 91 additions & 14 deletions module_1/2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,108 @@
#include <iostream>
#include <cassert>

// Инвертируйте значение бита в числе N по его номеру K.
// Необходимо использование битовых операций.
// Использование арифметических операций запрещено.
// Дан отсортированный массив различных целых чисел A[0..n-1] и массив целых чисел B[0..m-1].
// Для каждого элемента массива B[i] найдите минимальный индекс элемента массива A[k], ближайшего по
// значению к B[i]. n ≤ 110000, m ≤ 1000. Время работы поиска для каждого элемента B[i]: O(log(k)).

#define MAX_NUMBER (u_int32_t)(2e32 - 1)
#define MAX_BIT (u_int32_t)(32 - 1)
#define MAX_COUNT_ARRAY_FIRST 110000
#define MAX_COUNT_ARRAY_SECOND 1000

u_int32_t input(u_int32_t max) {
u_int32_t number = 0;
typedef struct {
size_t begin;
size_t end;
} interval_t;

std::cin >> number;
void exponential_search(const int key, const int *array, const size_t left, const size_t right, interval_t &intr) {
size_t begin = left;
size_t end = left + 1;

assert(max >= number);
while (end < right) {
if (key > array[end]) {
begin = end;

return number;
end *= 2;
} else {
intr.begin = begin;
intr.end = end;
return;
}
}

intr.begin = begin / 2;
intr.end = right - 1;
}

size_t binary_search(const int key, const int* array, size_t left, size_t right) {
if (key < array[left]) {
return left;
}

if (key > array[right]) {
return right;
}

while (left + 1 != right) {
size_t mid = (left + right) / 2;

if (array[mid] > key) {
right = mid;
} else {
left = mid;
}
}

if (key - array[left] > array[right] - key) {
return right;
} else {
return left;
}

}

int *input_array(size_t max_count, size_t &count) {
std::cin >> count;

assert(count <= max_count);

int *res = new int [count];
if (!res) {
return nullptr;
}

for (size_t i = 0; i < count; ++i) {
std::cin >> res[i];
}

return res;
}

int main() {
u_int32_t number = input(MAX_NUMBER);
size_t length_a = 0;
int *A = input_array(MAX_COUNT_ARRAY_FIRST, length_a);

size_t length_b = 0;
int *B = input_array(MAX_COUNT_ARRAY_SECOND, length_b);

interval_t *intr = new interval_t;
if (!intr) {
return -1;
}

for (size_t i = 0; i < length_b; ++i) {
exponential_search(B[i], A, 0, length_a, *intr);

size_t res = binary_search(B[i], A, intr->begin, intr->end);

std::cout << res << " ";
}

delete intr;

u_int32_t bit = input(MAX_BIT);
delete[] A;

u_int32_t res = number ^ (1 << bit);
delete[] B;

std::cout << res << std::endl;

return EXIT_SUCCESS;
}
14 changes: 4 additions & 10 deletions module_1/Example_2
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
3 0 0 2
10 20 30
3
9 15 35


3 0 0 0 2
10 20 30
4
8 9 10 32
10
10 20 30 40 50 60 100 200 4400 5000
10
0 2 10 19 21 151 220 4000 4999 5000
4 changes: 2 additions & 2 deletions run_linters.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ print_header "RUN cppcheck"
check_log "cppcheck module_1 --enable=all --inconclusive --error-exitcode=1 -I module_1 --suppress=missingIncludeSystem" "\(information\)"

print_header "RUN clang-tidy"
check_log "clang-tidy module_1/* -warnings-as-errors=* -extra-arg=-std=c99 -- " "Error (?:reading|while processing)"
check_log "clang-tidy module_1/*.cpp -warnings-as-errors=* -extra-arg=-std=c99 -- " "Error (?:reading|while processing)"

print_header "RUN cpplint"
check_log "cpplint --extensions=c module_1/* " "Can't open for reading"
check_log "cpplint --extensions=c module_1/*.cpp " "Can't open for reading"

print_header "SUCCESS"

0 comments on commit 22032f6

Please sign in to comment.