We participated in AtCorder Beginner Contest 170. It was ABC's 3 questions AC. I'm using Python3.
Put the input in the list and find the number 0 (index).
import sys
def input():
return sys.stdin.readline()[:-1]
def main():
A = list(map(int,input().split()))
print(A.index(0) + 1)
if __name__ == "__main__":
main()
It is a sword calculation. Assuming all animals are cranes, divide the number of remaining legs by the difference between the number of crane and turtle legs (4-2 = 2). However, since the sword calculation does not hold in all cases, consider an exception. 1 Too many / too few legs 2 Number of legs that cannot be combined (odd number, etc.)
import sys
def input():
return sys.stdin.readline()[:-1]
def main():
X, Y = map(int,input().split())
a = Y - 2 * X
if a >= 0 and a % 2 == 0 and a / 2 <= X:
print('Yes')
else:
print('No')
if __name__ == "__main__":
main()
Find the difference between the input value p and the integer X and create a new list p_new. The answer is 0 → ± 1 → ± 2 when you search the list repeatedly with a while statement and cannot find it. However, if there are multiple, the smaller one is answered, so the value obtained by subtracting the absolute value num is answered.
import sys
def input():
return sys.stdin.readline()[:-1]
def main():
X, N = map(int,input().split())
p = list(map(int,input().split()))
p_new = [i - X for i in p]
num = 0
while num in p_new and -num in p_new:
num += 1
if X - num in p:
print(X + num)
else:
print(X - num)
if __name__ == "__main__":
main()
Recommended Posts