-
백준 1111 - IQ test (Python)BOJ PS/Python 2023. 6. 1. 20:29
https://acmicpc.net/problem/1111
1111번: IQ Test
다음 수를 출력한다. 만약 다음 수가 여러 개일 경우에는 A를 출력하고, 다음 수를 구할 수 없는 경우에는 B를 출력한다.
www.acmicpc.net
사실상 그냥 다 하는 브루트포스이다.
숫자가 하나나 둘이면 바로 끝. 아니라면 하나하나 계산을 해보기 시작한다.
다만 어느 범위에 대해 해보아야 할 지가 고민이었는데, 질문게시판에서 능력자분들이 200이래서 그런갑다 했다..import sys input=sys.stdin.readline N=int(input()) lis=list(map(int,input().split())) answer=False if N==1: print("A") exit(0) elif N==2: if lis[0]==lis[1]: print(lis[0]) exit(0) else: print("A") exit(0) def try_ab(a,b,x,y): global ans,answer if b==None: b=lis[y]-a*lis[x] if a*lis[x]+b==lis[y]: if y==N-1: if ans==0: answer=(a,b) ans+=1 else: answer="A" return else: try_ab(a, b, x + 1, y + 1) else: return False for i in range(-200,199): ans=0 try_ab(i,None,0,1) if not answer: print("B") elif answer=="A": print(answer) else: print(lis[-1]*answer[0]+answer[1])
'BOJ PS > Python' 카테고리의 다른 글
최대 유량 공부 (0) 2023.06.04 백준 19238 - 스마트 택시 (Python) (0) 2023.06.01 백준 1744 - 수 묶기 (Python) (0) 2023.06.01 백준 3425 - 고스택 (Python) (0) 2023.06.01 백준 17136 - 색종이 붙이기 (Python) (0) 2023.06.01