1、题目描述
本关任务:m
个人的成绩存放在 score
数组中,请编写函数 fun
,它的功能是:将低于平均分的人数作为函数值返回,将低于平均分的人数放在 below
所指的数组中。
输入格式
个人成绩数组。
输出格式
低于平均分的成绩。
输入输出样例
输入1
10 20 30 40 50 60 70 80 90
输出1
10 20 30 40
2、数学分析
首先计算出平均成绩,记录低于平均成绩的分数。
3、代码实现
#include <stdio.h>
#include <string.h>
int fun(int score[],int m, int below[])
{
//avg为score数组的平均值,a为低于平均分人数
int sum=0,avg,a=0;
//求总数
for(int i=0;i<m;i++)
{
sum=sum+score[i];
}
//求平均分
avg=sum/m;
//将低于平均分的成绩赋值给below数组中,并记录人数
for(int j=0,k=0;j<m;j++)
{
if(score[j]<avg)
{
below[k]=score[j];
k++;
a++;
}
}
return a;
}
main()
{ int i,n,below[9];
int score[9];
for(i=0;i<9;i++)
scanf("%d",&score[i]);
n=fun(score,9,below);
for(i=0;i<n;i++)
printf("%d ",below[i]);
}
4、测试结果
10 20 30 40 50 60 70 80 90
执行用时:0
执行结果:
10 20 30 40
题目来源:头歌实践教学平台在线刷题题库