藍橋杯:整數平均值
阿新 • • 發佈:2018-12-24
題目描述
編寫函式,求包含n個元素的整數陣列中元素的平均值。要求在函式內部使用指標操縱陣列元素,其中n個整數從鍵盤輸入,輸出為其平均值。
(樣例說明:5為輸入資料的個數,3 4 0 0 2 是以空格隔開的5個整數)
輸入
無
輸出
無
樣例輸入
5 3 4 0 0 2
樣例輸出
1
程式設計程式碼如下:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
sum += arr[i];
}
System.out.println(sum / n);
}