[Java] 藍橋杯 BASIC-4 基礎練習 數列特徵
問題描述
給出n個數,找出這n個數的最大值,最小值,和。
輸入格式
第一行為整數n,表示數的個數。
第二行有n個數,為給定的n個數,每個數的絕對值都小於10000。
輸出格式
輸出三行,每行一個整數。第一行表示這些數中的最大值,第二行表示這些數中的最小值,第三行表示這些數的和。
樣例輸入
5
1 3 -2 4 5
樣例輸出
5
-2
11
資料規模與約定
1 <= n <= 10000。
package algorithm.Lanqiao.基礎練習; import java.util.Iterator; import java.util.Scanner; public class base4 { public static void main(String[] args) { int max, min, count; Scanner in = new Scanner(System.in); int i = 0; String str = in.nextLine(); String[] strs = str.split(" "); int[] num = new int[strs.length]; for (int j = 0; j < strs.length; j++) { num[j] = Integer.parseInt(strs[j]); } in.close(); max = min = count = num[0]; for (int j = 1; j < num.length; j++) { if (num[j] > max) { max = num[j]; } if (num[j] < min) { min = num[j]; } count += num[j]; } System.out.println(max); System.out.println(min); System.out.println(count); } }ofollow,noindex" target="_blank">❤❤點選這裡 -> 訂閱PAT、GPLT天梯賽、LeetCode題解離線版❤❤
