첫 줄에 날 수 n이, 둘째 줄에 날마다의 기온 n개가 날짜 순서대로 빈칸으로 띄워 주어진다.
어떤 날의 기온이 그날보다 앞선 모든 날의 기온보다 높으면 그날은 최고 기록을 경신한 날이다.
어떤 날의 기온이 그날보다 앞선 모든 날의 기온보다 낮으면 그날은 최저 기록을 경신한 날이다.
앞선 날의 기록과 같은 기온은 경신이 아니다.
첫날은 앞선 날이 없으므로 어느 쪽으로도 세지 않는다.
최고 기록을 경신한 날의 수와 최저 기록을 경신한 날의 수를 빈칸 하나로 띄워 한 줄에 출력하라.
입력
1 ≤ n ≤ 1,000, -50 ≤ 기온 ≤ 50
출력
최고 경신 횟수와 최저 경신 횟수 한 줄
예시 1 · stdin
7
4 9 2 9 1 10 2
stdout
2 2
예시 2 · stdin
5
-5 -3 -8 -1 -10
stdout
2 2
main.py
오류 목록00F8 ↓ · ⇧F8 ↑
입력(stdin)
실행하면 이 줄들이 차례로 input()·표준 입력으로 들어가요(실행 중에 묻지 않아요).
$ 준비됐어요
코드를 입력해 풀려면 PC에서 열어 주세요. 모바일에서는 문제와 풀이를 볼 수 있어요.
다른 언어로는?언어별 풀이 보기 · 8개 언어 풀이
번호 없이 목록의 값을 하나씩 받는 반복은 Python for t in temps, JS·TS for (const t of temps), Java for (int t : temps), C# foreach (int t in temps), C++ for (int t : temps), 크레이콘 각각 (t 에서 temps)이에요. C에는 이런 반복이 없어서 for (int i = 0; i < n; i++)로 번호를 돌며 temps[i]를 읽어요. 가장 헷갈리는 것은 JS·TS의 for...in이에요. 값이 아니라 번호 "0", "1", …을 문자열로 줘서 기온 대신 번호를 비교하게 돼요(TS는 문자열 번호를 수와 비교하는 곳에서 타입 오류로 알려 줘요). 최고·최저를 0으로 시작하면 영하인 날이 최고 경신으로 세어지지 않고 첫날이 경신으로 세어지니, 첫날 기온 temps[0]으로 시작해요. 이렇게 하면 첫날은 자기 자신보다 크지도 작지도 않아서 목록 전체를 돌아도 저절로 빠져요. 같은 기온을 세지 않으려면 >=가 아니라 >로 물어요. C++에서 값을 읽어 넣을 때는 for (int& t : temps)처럼 참조로 받아야 해요. int t로 받으면 복사본에 넣고 목록은 그대로 0이에요.
Pythonmain.py · 12 줄
n = int(input())temps = list(map(int, input().split()))high = low = temps[0] # 첫날 기온으로 시작한다(0이 아니라)highs = lows = 0for t in temps: if t > high: high = t highs += 1 elif t < low: low = t lows += 1print(highs, lows)
JavaScriptmain.js · 16 줄
const [n, ...temps] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let high = temps[0];let low = temps[0];let highs = 0;let lows = 0;// for...of는 값을, for...in은 번호(문자열)를 준다for (const t of temps) { if (t > high) { high = t; highs++; } else if (t < low) { low = t; lows++; }}console.log(`${highs} ${lows}`);
TypeScriptmain.ts · 16 줄
import { readFileSync } from "node:fs";const [n, ...temps]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let high: number = temps[0];let low: number = temps[0];let highs = 0;let lows = 0;for (const t of temps) { if (t > high) { high = t; highs++; } else if (t < low) { low = t; lows++; }}console.log(`${highs} ${lows}`);
JavaMain.java · 23 줄
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] temps = new int[n]; for (int i = 0; i < n; i++) temps[i] = sc.nextInt(); int high = temps[0], low = temps[0]; int highs = 0, lows = 0; // 향상된 for: 칸의 값을 하나씩 받는다 for (int t : temps) { if (t > high) { high = t; highs++; } else if (t < low) { low = t; lows++; } } System.out.println(highs + " " + lows); }}
C#Main.cs · 27 줄
using System;using System.Linq;class Program{ static void Main() { Console.ReadLine(); int[] temps = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray(); int high = temps[0], low = temps[0]; int highs = 0, lows = 0; foreach (int t in temps) { if (t > high) { high = t; highs++; } else if (t < low) { low = t; lows++; } } Console.WriteLine($"{highs} {lows}"); }}
C++main.cpp · 22 줄
#include <iostream>#include <vector>int main() { int n; std::cin >> n; std::vector<int> temps(n); for (int& t : temps) std::cin >> t; int high = temps[0], low = temps[0]; int highs = 0, lows = 0; // 범위 기반 for: 값을 하나씩 받는다 for (int t : temps) { if (t > high) { high = t; highs++; } else if (t < low) { low = t; lows++; } } std::cout << highs << " " << lows << "\n";}
Cmain.c · 23 줄
#include <stdio.h>int main(void) { int n; static int temps[1000]; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &temps[i]); int high = temps[0], low = temps[0]; int highs = 0, lows = 0; /* C에는 foreach가 없어서 번호로 돈다 */ for (int i = 0; i < n; i++) { int t = temps[i]; if (t > high) { high = t; highs++; } else if (t < low) { low = t; lows++; } } printf("%d %d\n", highs, lows); return 0;}
Craykonmain.cray · 20 줄
n = 콘솔.줄읽기().수로변환();temps = 콘솔.수들읽기();high = temps[0]; // 첫날 기온으로 시작한다low = temps[0];highs = 0;lows = 0;각각 (t 에서 temps){ 만약 (t > high) { high = t; highs++; } 아니면 만약 (t < low) { low = t; lows++; }}콘솔.쓰기(highs + " " + lows);