Форум » Кодирование и декодирование информации » задание8 №4929 не сходится ответ » Ответить

задание8 №4929 не сходится ответ

oladywki: (№ 4929) Определите количество семизначных чисел, записанных в девятеричной системе счисления, учитывая, что числа не могут начинаться с цифр 2, 4 и 6 и не должны заканчиваться на тройку одинаковых цифр (например, на 000). 2624400 Решение: from itertools import product A = '012345678' s = ["".join(x) for x in product(A,repeat=7) if x[0] not in '0246'] print(len([x for x in s if not('000' in x and '111' in x and '222' in x and '333' in x and '444' in x and '555' in x and '666' in x and '777' in x and '888' in x)])) ОТвет: 2657205

Ответов - 4

Фирсов М.: Ошибка в последней строчке когда not(and), лучше заменить на or, если везде ложь, только тогда записывать. Не встречаются только в конце, при этом могут встречаться в другом месте. print(len([x for x in s if not('000' in x[-3:] or '111' in x[-3:] or '222' in x[-3:] or '333' in x[-3:] or '444' in x[-3:] or '555' in x[-3:] or '666' in x[-3:] or '777' in x[-3:] or '888' in x[-3:])])) можно записать также print(len([x for x in s if not(x[-3] == x[-2] == x[-1])]))

oladywki: СПАСИБО

reaktiv: from itertools import product cnt = 0 for i in product('012345678', repeat = 7): a = ''.join(i) if (i[0] != '0' and i[0] != '2' and i[0] != '4' and i[0] != '6') and not(i[-1] == i[-2] == i[-3]): cnt += 1 print(cnt) cnt = 0 for i1 in '012345678': for i2 in '012345678': for i3 in '012345678': for i4 in '012345678': for i5 in '012345678': for i6 in '012345678': for i7 in '012345678': if (i1 != '0' and i1 != '2' and i1 != '4' and i1 != '6') and not ( i7 == i6 == i5): cnt += 1 print(cnt)


s11kai: reaktiv пишет: from itertools import product cnt = 0 for i in product('012345678', repeat = 7): a = ''.join(i) if (i[0] != '0' and i[0] != '2' and i[0] != '4' and i[0] != '6') and not(i[-1] == i[-2] == i[-3]): cnt += 1 print(cnt) Красивое решение, но его можно чуток укоротить, например так: [pre2]from itertools import product cnt = 0 for i in product('012345678', repeat = 7): n = ''.join(i) if n[0] not in '0246' and not (n[-3] == n[-2] == n[-1]): cnt += 1 print(cnt) или так: from itertools import product cnt = [] for i in product('012345678', repeat = 7): n = ''.join(i) if n[0] not in '0246' and not (n[-3] == n[-2] == n[-1]): cnt.append(n) print(len(cnt)) и это, уменьшает количество набираемых буковок cnt,s = 0,'012345678' for i1 in s: for i2 in s: for i3 in s: for i4 in s: for i5 in s: for i6 in s: for i7 in s: if i1 not in '0246' and not (i7 == i6 == i5): cnt += 1 print(cnt) [/pre2]



полная версия страницы