6-1 统计平均分,最高分及得最高分人数 分数 15 作者 王跃萍 单位 东北石油大学 请编写一个函数fun,函数的功能是:统计出若干个学生的平均成绩,最高分以及得最高分的人数。 函数接口定义: float fun(float array[],int n); 其中 array 和 n 都是用户传入的参数。函数须统计 array数组中学生的平均成绩,最高分以及得最高分的人数,函数返回平均成绩,最高分以及得最高分的人数存放在全局变量 Max 和 J中 。 裁判测试程序样例:
时间: 2023-06-13 18:09:07 浏览: 86
```c
#include <stdio.h>
float Max,J;
float fun(float array[],int n);
int main()
{
float array[100];
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%f",&array[i]);
printf("%.2f %d",fun(array,n),J);
return 0;
}
float fun(float array[],int n)
{
int i,count=0;
float sum=0;
Max=array[0];
for(i=0;i<n;i++)
{
sum+=array[i];
if(array[i]>Max)
Max=array[i];
}
for(i=0;i<n;i++)
{
if(array[i]==Max)
count++;
}
J=count;
return sum/n;
}
```
注意事项:
1. 本题需要使用全局变量 Max 和 J,以便在函数外输出最高分和得最高分的人数。
2. 最高分可能有多个人,需要统计人数。
相关问题
6-1 统计平均分,最高分及得最高分人数 分数 15 作者 王跃萍 单位 东北石油大学 请编写一个函数fun,函数的功能是:统计出若干个学生的平均成绩,最高分以及得最高分的人数。
### 回答1:
以下是函数fun的实现:
```c
#include <stdio.h>
void fun(float *score, int n, float *average, float *max_score, int *max_num) {
int i;
float sum = 0.0;
*max_score = 0.0;
*max_num = 0;
// 计算平均分和最高分
for (i = 0; i < n; i++) {
sum += score[i];
if (score[i] > *max_score) {
*max_score = score[i];
}
}
*average = sum / n;
// 计算得最高分的人数
for (i = 0; i < n; i++) {
if (score[i] == *max_score) {
(*max_num)++;
}
}
}
int main() {
float score[] = {85.5, 92.0, 78.5, 99.5};
float average, max_score;
int max_num;
fun(score, 4, &average, &max_score, &max_num);
printf("平均分为:%.2f\n", average);
printf("最高分为:%.2f\n", max_score);
printf("得最高分的人数为:%d\n", max_num);
return 0;
}
```
在主函数中可以调用fun函数来统计学生的平均成绩,最高分以及得最高分的人数。调用fun函数需要提供一个数组score,表示学生的成绩,一个整数n,表示学生的数量,以及三个指针变量,用于存储平均分、最高分和得最高分的人数。fun函数的实现过程中,首先计算平均分和最高分,然后再计算得最高分的人数。最后,主函数输出平均分、最高分和得最高分的人数。
### 回答2:
要编写一个函数fun,实现统计若干个学生的平均成绩、最高分以及得最高分的人数的功能。函数的输入参数是一个包含各个学生分数的列表,函数的输出结果是一个包含平均成绩、最高分以及得最高分的人数的元组。
具体的实现步骤如下:
1. 创建一个变量total,用于存储所有学生的总分,初始值为0。
2. 创建一个变量count,用于统计学生的人数,初始值为0。
3. 创建一个变量max_score,用于存储最高分,初始值为列表中第一个元素的值。
4. 创建一个变量max_score_count,用于统计得最高分的人数,初始值为1。
5. 使用for循环遍历列表中的每个分数,将分数累加到total中,将count加1。
6. 在每次遍历时,比较当前分数与max_score的大小,如果大于max_score,则更新max_score的值,并将max_score_count重置为1;如果等于max_score,则将max_score_count加1。
7. 计算平均成绩,将total除以count,并保留两位小数。
8. 返回一个包含平均成绩、最高分和得最高分的人数的元组,元组的三个元素分别为平均成绩、最高分以及得最高分的人数。
以下是函数fun的完整代码:
```python
def fun(scores):
total = 0
count = 0
max_score = scores[0]
max_score_count = 1
for score in scores:
total += score
count += 1
if score > max_score:
max_score = score
max_score_count = 1
elif score == max_score:
max_score_count += 1
average_score = round(total / count, 2)
result = (average_score, max_score, max_score_count)
return result
```
可以通过调用fun函数,传入包含学生分数的列表来获取平均成绩、最高分以及得最高分的人数:
```python
scores = [15, 16, 17, 18, 15, 20, 19, 18, 17]
result = fun(scores)
print("平均成绩:", result[0])
print("最高分:", result[1])
print("得最高分的人数:", result[2])
```
运行结果如下:
平均成绩: 17.22
最高分: 20
得最高分的人数: 1
### 回答3:
可以定义一个函数`fun`,这个函数的输入为一个学生列表`students`,每个学生的成绩存储在一个字典中,包含名字和分数。函数的输出为平均分、最高分以及得最高分的人数。
具体实现如下:
```
def fun(students):
# 初始化变量
total_score = 0 # 总分
max_score = 0 # 最高分
max_score_count = 0 # 最高分的人数
# 统计平均分和最高分
for student in students:
total_score += student['score']
if student['score'] > max_score:
max_score = student['score']
max_score_count = 1 # 初始化最高分的人数为1
elif student['score'] == max_score:
max_score_count += 1 # 最高分的人数加1
# 计算平均分
average_score = total_score / len(students)
# 返回结果
return average_score, max_score, max_score_count
```
假设有如下的学生列表:
```
students = [{'name': '张三', 'score': 80},
{'name': '李四', 'score': 90},
{'name': '王五', 'score': 90},
{'name': '赵六', 'score': 85}]
```
调用函数`fun`进行统计:
```
average, max_score, max_score_count = fun(students)
print("平均分为:", average)
print("最高分为:", max_score)
print("得最高分的人数为:", max_score_count)
```
输出结果为:
```
平均分为: 86.25
最高分为: 90
得最高分的人数为: 2
```
gcc 编写函数计算Sn=a+aa+aaa+…+aa…a 分数 10 作者 王跃萍 单位 东北石油大学 编写函数计算Sn=a+aa+aaa+…+aa…a(最后一项为n个a)。
以下是使用C++编写函数计算Sn=a+aa+aaa+…+aa…a的代码:
```c++
#include <iostream>
#include <cmath>
using namespace std;
int calc(int a, int n) {
int sum = 0;
int temp = a;
for (int i = 1; i <= n; i++) {
sum += temp;
temp = temp * 10 + a;
}
return sum;
}
int main() {
int a, n;
cout << "请输入a和n的值:";
cin >> a >> n;
int result = calc(a, n);
cout << "Sn的值为:" << result << endl;
return 0;
}
```
运行结果如下:
```
请输入a和n的值:2 3
Sn的值为:246
```