问题描述 给出n个数,找出这n个数的最大值,最小值,和。 输入格式 第一行为整数n,表示数的个数。 第二行有n个数,为给定的n个数,每个数的绝对值都小于10000。 输出格式 输出三行,每行一个整数。第一行表示这些数中的最大值,第二行表示这些数中的最小值,第三行表示这些数的和。 样例输入 5 1 3 -2 4 5 样例输出 5 -2 11 数据规模与约定 1 <= n <= 10000。
解题源代码如下:
import java.util.Scanner; //创建于20190304 //为算法刷题模板 class Main { private static Scanner sc = new Scanner(System.in); public static void main (String[] args){ int counter = sc.nextInt();//receive the gross of the array; int root[] = new int[counter]; for(int i = 0;i<counter;i++){ root[i] = sc.nextInt();//receive all of the array } int min =root[0],max = root[0],sum = 0;//define all the output together for(int i = 0;i<counter;i++){ sum+= root[i]; min = min>root[i]?root[i]:min; max = max<root[i]?root[i]:max; } System.out.println(max+" "+min+" "+sum); } }
额,解题思路过于简单,看源码就好了
以上
希望对大家有所帮助