输入10个数,并求出这10个数的和及平均值。 例如: 输入: 5 5 5 3 2 2 6 8 1 2 输出: 和为:39.0,平均值为:3.9
时间: 2023-06-11 09:08:35 浏览: 66
好的,以下是 Python 的代码实现:
```python
nums = input("请输入10个数,以空格分隔:").split()
nums = [float(num) for num in nums] # 将输入的字符串转换为浮点数列表
total = sum(nums) # 计算总和
average = total / len(nums) # 计算平均值
print(f"和为:{total},平均值为:{average:.1f}")
```
运行示例:
```
请输入10个数,以空格分隔:5 5 5 3 2 2 6 8 1 2
和为:39.0,平均值为:3.9
```
相关问题
编写程序, 先输入一组10个元素,再输出数组中比平均值小的所有奇数。 例如:输入:1 2 3 4 5 6 7 8 9 10 输出:1 3 5
以下是 Python 代码实现:
```python
arr = list(map(int, input("请输入10个整数,以空格分隔: ").split()))
avg = sum(arr) / len(arr) # 计算平均值
result = []
for num in arr:
if num % 2 == 1 and num < avg:
result.append(num)
print("比平均值小的所有奇数为:", end=" ")
print(*result) # 使用 * 将列表展开为多个参数输出
```
示例输出:
```
请输入10个整数,以空格分隔: 1 2 3 4 5 6 7 8 9 10
比平均值小的所有奇数为: 1 3 5
```
有6组数,每组3个数,按组输入值,并求出每组数的平均值,C语言解决
好的,我可以帮你解决这个问题。以下是一个使用C语言的例子代码,用于输入6组3个数,并计算每组数的平均值:
```c
#include <stdio.h>
int main() {
int numbers[6][3];
float averages[6];
// 输入每组数
for (int i = 0; i < 6; i++) {
printf("输入第 %d 组数:\n", i + 1);
for (int j = 0; j < 3; j++) {
printf("输入第 %d 个数:", j + 1);
scanf("%d", &numbers[i][j]);
}
}
// 计算每组数的平均值
for (int i = 0; i < 6; i++) {
int sum = 0;
for (int j = 0; j < 3; j++) {
sum += numbers[i][j];
}
averages[i] = (float)sum / 3;
}
// 输出每组数的平均值
printf("每组数的平均值:\n");
for (int i = 0; i < 6; i++) {
printf("第 %d 组数的平均值为 %.2f\n", i + 1, averages[i]);
}
return 0;
}
```
你可以将这段代码保存为一个C语言源文件(例如`average.c`),然后编译和运行它。在运行时,依次输入每组数的3个数字,然后程序会计算出每组数的平均值并输出。
希望这可以帮到你!如果还有其他问题,请随时提问。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)