练习3-3 统计学生平均成绩与及格人数 (15point(s)).c

本题要求编写程序,计算学生们的平均成绩,并统计及格(成绩不低于60分)的人数。题目保证输入与输出均在整型范围内。

输入格式:
输入在第一行中给出非负整数N,即学生人数。第二行给出N个非负整数,即这N位学生的成绩,其间以空格分隔。

输出格式:
按照以下格式输出:

average = 成绩均值
count = 及格人数

其中平均值精确到小数点后一位。

输入样例:
5
77 54 92 73 60

输出样例:
average = 71.2
count = 4
Common problems are as follows:
(1)The input of 0 is not considered;
(2) the input of 0 is considered, but the requirement that the average value of the subject should keep one decimal place is not considered.

在这里插入图片描述
The correct answer is as follows:

//   Date:2020/3/16
//   Author:xiezhg5
#include <stdio.h>
#include <math.h>
int main(void)
{
	int i,k,count=0;
	int n;
	int sum=0;
	double average;
	scanf("%d",&n);
    if(n==0)
    {
    	printf("average = 0.0\n");
    	printf("count = 0\n");
	}
	else
	{
		for(i=0;i<n;i++)
		{
			scanf("%d",&k);
			sum=sum+k;
			if(k>=60)
			count++;
		}
	    average=1.0*sum/n;
	    printf("average = %.1lf\n",average);
	    printf("count = %d\n",count);
    }
	return 0;
}

在这里插入图片描述

发布了55 篇原创文章 · 获赞 27 · 访问量 1699

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/104909707