[SW11736] 평범한 숫자
✏️ 𝗔𝗹𝗴𝗼𝗿𝗶𝘁𝗵𝗺/SW Expert Academy

[SW11736] 평범한 숫자

 

 SW Expert Academy- 평범한 숫자 

문제 링크

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AXhh-H-KwUcDFARQ 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

문제 입력

2
3
1 3 2
5
1 3 5 4 2
#1 0
#2 2

 

문제 풀이

import sys

"""
[입력]
T
n: p 수열의 개수
p: (n개) 수열
return 평범한 숫자의 개수

[주의]
- 최소도 최대도 아닌 평범한 숫자
"""

sys.stdin = open('input.txt', 'rt')
T = int(input())

for t in range(T):
    n = int(input())
    p = list(map(int, input().split()))
    ans = 0

    for i in range(1, n - 1):
        _pi, pi, pi_ = p[i - 1], p[i], p[i + 1]
        if any([_pi < pi < pi_, pi_ < pi < _pi]):
            ans += 1

    print(f"#{t + 1} {ans}")

모든 수열을 둘러 보는 것이 아니라 p[i-1], p[i], p[i+1] 이렇게 3개씩 봐야합니다.

 

 

 

 

 

 

 

 

 

# SW Expert 평범한숫자 Python python 파이썬 풀이


 

728x90