3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

問題文

解答例

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			int min = scanner.nextInt();
			for (int i = 0; i < 4; i++) {
				min = Integer.min(min, scanner.nextInt());
			}
			System.out.println(min);
		}
	}
}

補足

JavaのStreamを利用してみた別解が以下。

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			List<Integer> list = new ArrayList<>();
			for (int i = 0; i < 5; i++) {
				list.add(scanner.nextInt());
			}
			System.out.println(list.stream().min(Comparator.naturalOrder()).orElseThrow());
		}
	}
}

一応上記でも動く……が、このやり方だと値を全てlist上に保持していくことになるため、比較する値の個数が多くなっていくとどこかでOutOfMemoryErrorとかが発生するはず。その場合は大人しく最初の解答例のような実装(不要な値は捨てていく)にするほうが良い。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?