输入10个同学成绩,求高于平均分的人数
时间: 2023-12-15 08:03:42 浏览: 52
我可以为您提供一个Python代码实现:
scores = [] # 用一个列表来存储成绩
# 输入10个同学的成绩
for i in range(10):
score = float(input("请输入第{}个同学的成绩:".format(i+1)))
scores.append(score)
# 求平均分
average = sum(scores) / len(scores)
# 统计高于平均分的人数
count = 0
for score in scores:
if score > average:
count += 1
print("平均分为:{:.2f}".format(average))
print("高于平均分的人数为:{}".format(count))
您可以将以上代码复制到Python环境中运行,然后输入10个同学的成绩即可得到高于平均分的人数。
相关问题
输入10个同学成绩,求高于平均分的人数,C语言
#include <stdio.h>
int main() {
int i, sum = 0, count = 0;
float average;
int scores[10];
// 输入成绩
printf("请输入10个同学的成绩:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &scores[i]);
sum += scores[i]; // 累加成绩
}
average = (float)sum / 10; // 计算平均分
// 统计高于平均分的人数
for (i = 0; i < 10; i++) {
if (scores[i] > average) {
count++;
}
}
// 输出结果
printf("平均分为%.2f,高于平均分的人数为%d\n", average, count);
return 0;
}
输入10个同学成绩,求高于平均分的人数,C语言调用函数
#include <stdio.h>
// 声明函数
float average(float arr[], int n); // 求平均数
int above_average(float arr[], int n); // 求高于平均数的人数
int main()
{
float scores[10]; // 存储成绩的数组
int i;
printf("请输入10个同学的成绩:\n");
for(i = 0; i < 10; i++)
{
scanf("%f", &scores[i]);
}
float avg = average(scores, 10); // 求平均数
printf("平均分为:%.2f\n", avg);
int above_avg_num = above_average(scores, 10); // 求高于平均数的人数
printf("高于平均分的人数为:%d\n", above_avg_num);
return 0;
}
// 定义函数
float average(float arr[], int n)
{
int i;
float sum = 0;
for(i = 0; i < n; i++)
{
sum += arr[i];
}
return sum / n;
}
int above_average(float arr[], int n)
{
int i, count = 0;
float avg = average(arr, n);
for(i = 0; i < n; i++)
{
if(arr[i] > avg)
{
count++;
}
}
return count;
}
相关推荐
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)