試題 算法訓練 動態數組使用
資源限制
時間限制:1.0s 內存限制:512.0MB
從鍵盤讀入n個整數,使用動態數組存儲所讀入的整數,並計算它們的和與平均值分別輸出。要求儘可能使用函數實現程序代碼。平均值爲小數的只保留其整數部分。
樣例輸入:
5
3 4 0 0 2
樣例輸出:
9 1
樣例輸入:
7
3 2 7 5 2 9 1
樣例輸出:
29 4
package Main;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for (int i = 0; i < n; i++) {
a[i]=sc.nextInt();
}
int s=0;
int bb=0;
for (int j = 0; j < a.length; j++) {
s=s+a[j];
}
System.out.print(s+" "+s/n);
sc.close();
}
}