-
-
Save BOTKooper/60b99bb7c01edbacc39a9179084fa46d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Дан одномерный массив целых положительных и | |
# отрицательных чисел. Вместо этого массива создайте | |
# массив, в котором элемент равняется 1, если он | |
# больше или равный 0, и нулю, если число | |
# отрицательное. | |
# N= 6 -12 33 5 0 -3 7 | |
def main(): | |
N = int(input("Введите количество элементов массива: ")) | |
print("Введите элементы массива по одному: ") | |
arr = [int(input()) for i in range(N)] | |
out = [] | |
for i in range(N): | |
if arr[i] >= 0: | |
out.append(1) | |
else: | |
out.append(0) | |
print(out) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# N целых чисел размещенные в порядке | |
# увеличения. На какой позиции находится | |
# число с? | |
# 4,5,6,8,10 | |
# с=6 | |
# 2,3,5,7,9,11 | |
# с=9 | |
def binary_search(arr, x): | |
left = 0 | |
right = len(arr) - 1 | |
while left <= right: | |
mid = (left + right) // 2 | |
if arr[mid] == x: | |
return mid | |
elif arr[mid] < x: | |
left = mid + 1 | |
else: | |
right = mid - 1 | |
return -1 | |
def main(): | |
N = int(input("Введите количество элементов массива: ")) | |
print("Введите элементы массива по одному: ") | |
arr = [int(input()) for i in range(N)] | |
x = int(input("Введите число, которое нужно найти: ")) | |
pos = binary_search(arr, x) | |
if pos >= 0: | |
print("Элемент находится на позиции", pos) | |
else: | |
print("Элемент не найден") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment