Java实现 蓝桥杯 算法训练 动态数组使用

算法训练 动态数组使用
时间限制:1.0s 内存限制:512.0MB
提交此题
从键盘读入n个整数,使用动态数组存储所读入的整数,并计算它们的和与平均值分别输出。要求尽可能使用函数实现程序代码。平均值为小数的只保留其整数部分。
样例输入:
5
3 4 0 0 2
样例输出:
9 1
样例输入:
7
3 2 7 5 2 9 1
样例输出:
29 4

import java.util.Scanner;


public class 动态数组使用 {
	public static void main(String[] args)  {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		int a[] = new int[n];
		int sum = 0;
		for (int i = 0; i < a.length; i++) {
			a[i] = sc.nextInt();
			sum += a[i];
		}
		int avg = sum/n;
		System.out.println(sum+" "+avg);
	}


}

原文地址:https://www.cnblogs.com/a1439775520/p/13079378.html