問題
B – 錠
回答
前方向と後ろ方向に全探索します。
a = int(input()) b = int(input()) def next_digit(num): if num == 9: return 0 else: return num + 1 def prev_digit(num): if num == 0: return 9 else: return num - 1 count_red = 0 a_tmp = a while a_tmp != b: count_red += 1 a_tmp = next_digit(a_tmp) count_blue = 0 a_tmp = a while a_tmp != b: count_blue += 1 a_tmp = prev_digit(a_tmp) print(min(count_red, count_blue))
数字は全部で10個あるので、片方の差が分かれば、もう一つの差は10からその片方の差を引くことで得ることができます。
a = int(input()) b = int(input()) diff_1 = abs(a-b) diff_2 = 10 - diff_1 print(min(diff_1, diff_2))