一行目に日数 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()・標準入力に渡されます(実行中に入力を求めません)。
$ 準備完了
コードを入力して解くにはパソコンで開いてください。モバイルでは問題と解答を見られます。
ほかの言語では?言語別の解答を見る · 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)、Craykon それぞれ (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;}