输入n个正数,计算这些数的算术平均值和几何平均值。 ( 要求n>=10)
时间: 2024-05-06 22:19:42 浏览: 10
算术平均值即为所有数的和除以数的个数,几何平均值即为所有数的乘积的n次方根(n为数的个数)。
以下是Python实现:
```python
n = int(input("请输入正数的个数:"))
if n < 10:
print("请输入大于等于10个正数!")
else:
nums = []
for i in range(n):
num = float(input("请输入第{}个正数:".format(i+1)))
nums.append(num)
# 计算算术平均值
avg = sum(nums) / n
print("算术平均值为:{}".format(avg))
# 计算几何平均值
product = 1.0
for num in nums:
product *= num
geo_mean = product ** (1/n)
print("几何平均值为:{}".format(geo_mean))
```
输出示例:
```
请输入正数的个数:10
请输入第1个正数:1
请输入第2个正数:2
请输入第3个正数:3
请输入第4个正数:4
请输入第5个正数:5
请输入第6个正数:6
请输入第7个正数:7
请输入第8个正数:8
请输入第9个正数:9
请输入第10个正数:10
算术平均值为:5.5
几何平均值为:4.528728688116765
```
相关问题
c语言输入n个正数,计算这些数的算术 平均值和几何平均值。 (要求n>=10)
```c
#include <stdio.h>
#include <math.h>
int main()
{
int n;
double sum = 0, prod = 1, num;
printf("请输入正数的个数(至少为10):");
scanf("%d", &n);
if (n < 10) {
printf("正数个数不足10个,程序退出。\n");
return 0;
}
for (int i = 1; i <= n; i++) {
printf("请输入第%d个正数:", i);
scanf("%lf", &num);
sum += num;
prod *= num;
}
double avg = sum / n;
double geom_avg = pow(prod, 1.0 / n);
printf("这%d个正数的算术平均值为:%.2lf\n", n, avg);
printf("这%d个正数的几何平均值为:%.2lf\n", n, geom_avg);
return 0;
}
```
输入n个正数,计算这些数的算术平均值和几何平均值 n≥10
算术平均值的计算公式为:$\frac{a_1+a_2+...+a_n}{n}$
几何平均值的计算公式为:$(a_1 \times a_2 \times ... \times a_n)^{\frac{1}{n}}$
其中,$a_i$ 代表第 $i$ 个正数。
Python 代码实现如下:
```python
n = int(input("请输入正数个数:"))
if n < 10:
print("n需要大于等于10")
else:
nums = []
for i in range(n):
num = float(input("请输入第{}个正数:".format(i+1)))
nums.append(num)
arithmetic_mean = sum(nums) / n
geometric_mean = 1
for num in nums:
geometric_mean *= num
geometric_mean **= 1/n
print("算术平均值为:{:.2f}".format(arithmetic_mean))
print("几何平均值为:{:.2f}".format(geometric_mean))
```
运行结果如下:
```
请输入正数个数:15
请输入第1个正数:2
请输入第2个正数:3
请输入第3个正数:4
请输入第4个正数:5
请输入第5个正数:6
请输入第6个正数:7
请输入第7个正数:8
请输入第8个正数:9
请输入第9个正数:10
请输入第10个正数:11
请输入第11个正数:12
请输入第12个正数:13
请输入第13个正数:14
请输入第14个正数:15
请输入第15个正数:16
算术平均值为:9.40
几何平均值为:7.91
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)