25 lines
478 B
C++
25 lines
478 B
C++
// 输入一个整数 n,然后输入 n 个整数,输出它们的平均值。
|
|
// 方法 2
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
int main()
|
|
{
|
|
int n;
|
|
double avg = 0;
|
|
int nums[100];
|
|
cout << "请输入整数 n: ";
|
|
cin >> n;
|
|
int n_copy = n;
|
|
|
|
cout << "请输入 " << n << " 个整数: ";
|
|
while (--n >= 0)
|
|
{
|
|
cin >> nums[n];
|
|
avg += nums[n] / (double)n_copy;
|
|
}
|
|
|
|
cout << "平均值为:" << avg << endl;
|
|
return 0;
|
|
} |