{ }codelper
for-each 循环P0219

数气温纪录被刷新几次

第一行给出天数 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
输入(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;}
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);