1. 程式人生 > >[Java] 藍橋杯 BASIC-4 基礎練習 數列特徵

[Java] 藍橋杯 BASIC-4 基礎練習 數列特徵

問題描述給出n個數,找出這n個數的最大值,最小值,和。

輸入格式第一行為整數n,表示數的個數。

第二行有n個數,為給定的n個數,每個數的絕對值都小於10000。

輸出格式輸出三行,每行一個整數。第一行表示這些數中的最大值,第二行表示這些數中的最小值,第三行表示這些數的和。樣例輸入51 3 -2 4 5樣例輸出5-211資料規模與約定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);
    }
}