дано масив цілих чисел: 6,8,13,17,19,30,13,8. розробіть програму визначення всіх позицій,на яких знаходиться число, значення якого вводиться з клавіатури
Ответы на вопрос
Ответ:
import math
mas = [6, 8, 13, 17,
19, 30, 13, 8]
n = len(mas)
c = int(input("Введите число: "))
per = False
l = 0
p = n-l
while ((l <= p) and (not per)):
i = int(math.floor(l+p)/2)
if mas[i] == c:
per = True
break
else:
if c>mas[i]:
l = i+l
else:
p = i+l
if per:
print("Число находится на позиции", i)
c1 = int(input("Введите число: "))
n1 = len(mas)
per1 = False
l1 = 0
p1 = n1-l1
while ((l1 <= p1) and (not per1)):
i1 = int(math.floor(l1+p1)/2)
if mas[i1] == c1:
per1 = True
break
else:
if c1<mas[i1]:
l1 = i1+l1
else:
p1 = i1+l1
if per1:
print("Число находится позиции", i1)
input()
Объяснение:
В скобках после print поменяйте слова как-то и будет норм)
