The first line gives the number of days n, and the second line gives n daily temperatures in date order, separated by spaces.
If a day's temperature is higher than every earlier day's, that day set a new high record.
If a day's temperature is lower than every earlier day's, that day set a new low record.
A temperature equal to an earlier record is not a new record.
The first day has no earlier days, so it counts as neither.
Print the number of new-high days and the number of new-low days on one line, separated by one space.
Input
1 ≤ n ≤ 1,000, -50 ≤ temperature ≤ 50
Output
The new-high count and the new-low count on one line
Example 1 · stdin
7
4 9 2 9 1 10 2
stdout
2 2
Example 2 · stdin
5
-5 -3 -8 -1 -10
stdout
2 2
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
A loop that takes each value of a list without an index is 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) and Craykon foreach (t in temps). C has no such loop, so you walk the index with for (int i = 0; i < n; i++) and read temps[i]. The most confusing one is for...in in JS/TS. It gives the indexes "0", "1", … as strings instead of the values, so you end up comparing indexes instead of temperatures (TS reports a type error where a string index is compared with a number). Starting the high and low at 0 misses new highs on days below zero and counts the first day as a record, so start from the first day's temperature temps[0]. Then the first day is neither higher nor lower than itself, so it drops out on its own even when you loop over the whole list. To skip equal temperatures, ask >, not >=. When reading values in C++, take a reference as in for (int& t : temps). With int t you fill a copy and the list stays all 0.
Pythonmain.py · 12 lines
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 lines
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 lines
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 lines
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 lines
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 lines
#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 lines
#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 lines
n = Console.ReadLine().ToNumber();temps = Console.ReadNumbers();high = temps[0]; // 첫날 기온으로 시작한다low = temps[0];highs = 0;lows = 0;foreach (t in temps){ if (t > high) { high = t; highs++; } else if (t < low) { low = t; lows++; }}Console.WriteLine(highs + " " + lows);