C语言习题 学生成绩

C语言习题 学生成绩

时间: 1ms        内存:128M

描述:

有一个班4个学生,5门课。①求第1门课的平均分;②找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩和平均成绩;③找出平均成绩在90分以上或全部课程成绩在85分以上的学生。分别编3个函数实现以上3个要求。

输入:

5门课的名称
4个学生的学号和5门课成绩

输出:

按题目要求

示例输入:

english math c++ chinese art
1001 80 90 100 70 50
1002 59 60 70 60 43
1003 90 100 85 90 90
1004 85 86 89 90 88

示例输出:

course 1:english,average score:78.50
======Student who failed in two courses======
NO.english math c++ chinese art average
1002 59.00 60.00 70.00 60.00 43.00 58.40
======Students whose score is good======
NO.english math c++ chinese art average
1003 90.00 100.00 85.00 90.00 90.00 91.00

提示:

参考答案(内存最优[752]):

#include<stdio.h>
void avsco(float *pscore,float *paver)
{
	int i,j;
	float S,ave;
	for(i=0;i<4;i++)
	{
		S=0.0;
		for(j=0;j<5;j++)
		{
			S+=(*(pscore+5*i+j));
			ave=S/5;
			*(paver+i)=ave;
		}
	}
}
void avcour1(char (*pcourse)[10],float *pscore)
{
	int i;
	float S,ave1;
	S=0.0;
	for(i=0;i<4;i++)
		S+=(*(pscore+5*i));
	ave1=S/4;
	printf("course 1:%s,average score:%.2f\n",*pcourse,ave1);
}

void fail2(char course[5][10],int num[],float *pscore,float aver[4])
{
	int i,j,k,l;
	printf("======Student who failed in two courses======\n");
	printf("NO.");
	for(i=0;i<5;i++)
		printf("%s ",course[i]);
	printf("average\n");
	for(i=0;i<4;i++)
	{
		l=0;
		for(j=0;j<5;j++)
			if(*(pscore+5*i+j)<60.0) l++;
			if(l>=2)
			{
				printf("%d ",num[i]);
				for(k=0;k<5;k++)
					printf("%.2f ",*(pscore+5*i+k));
				printf("%.2f\n",aver[i]);
			}
	}
}

void good(char course[5][10],int num[4],float *pscore,float aver[4])
{
	int i,j,k,n;
	printf("======Students whose score is good======\n");
	printf("NO.");
	for(i=0;i<5;i++)
		printf("%s ",course[i]);
	printf("average\n");
	for(i=0;i<4;i++)
	{
		n=0;
		for(j=0;j<5;j++)
			if(*(pscore+5*i+j)>85.0) n++;
			if((n==5)||(aver[i]>=90))
			{
				printf("%d ",num[i]);
				for(k=0;k<5;k++)
					printf("%.2f ",*(pscore+5*i+k));
				printf("%.2f\n",aver[i]);
			}
	}

}

int main()
{
    void avsco(float *,float *);
    void avcour1(char (*)[10],float *);
    void fail2(char course[5][10],int num[],float *pscore,float aver[4]);
    void good(char course[5][10],int num[4],float *pscore,float aver[4]);
    int i,j,*pnum,num[4];
    float score[4][5],aver[4],*pscore,*paver;
    char course[5][10],(*pcourse)[10];
    pcourse=course;
    for (i=0; i<5; i++)
		scanf("%s",course[i]);
    pscore=&score[0][0];
    pnum=&num[0];
    for (i=0; i<4; i++)
    {        
		scanf("%d",pnum+i);
        for (j=0; j<5; j++)
			scanf("%f",pscore+5*i+j);
    }
    paver=&aver[0];
    avsco(pscore,paver);        
    avcour1(pcourse,pscore);    
    fail2(pcourse,pnum,pscore,paver);  
    good(pcourse,pnum,pscore,paver);   
    return 0;
}

参考答案(时间最优[0]):

#include<stdio.h>
void avsco(float *pscore,float *paver)
{
	int i,j;
	float S,ave;
	for(i=0;i<4;i++)
	{
		S=0.0;
		for(j=0;j<5;j++)
		{
			S+=(*(pscore+5*i+j));
			ave=S/5;
			*(paver+i)=ave;
		}
	}
}
void avcour1(char (*pcourse)[10],float *pscore)
{
	int i;
	float S,ave1;
	S=0.0;
	for(i=0;i<4;i++)
		S+=(*(pscore+5*i));
	ave1=S/4;
	printf("course 1:%s,average score:%.2f\n",*pcourse,ave1);
}

void fail2(char course[5][10],int num[],float *pscore,float aver[4])
{
	int i,j,k,l;
	printf("======Student who failed in two courses======\n");
	printf("NO.");
	for(i=0;i<5;i++)
		printf("%s ",course[i]);
	printf("average\n");
	for(i=0;i<4;i++)
	{
		l=0;
		for(j=0;j<5;j++)
			if(*(pscore+5*i+j)<60.0) l++;
			if(l>=2)
			{
				printf("%d ",num[i]);
				for(k=0;k<5;k++)
					printf("%.2f ",*(pscore+5*i+k));
				printf("%.2f\n",aver[i]);
			}
	}
}

void good(char course[5][10],int num[4],float *pscore,float aver[4])
{
	int i,j,k,n;
	printf("======Students whose score is good======\n");
	printf("NO.");
	for(i=0;i<5;i++)
		printf("%s ",course[i]);
	printf("average\n");
	for(i=0;i<4;i++)
	{
		n=0;
		for(j=0;j<5;j++)
			if(*(pscore+5*i+j)>85.0) n++;
			if((n==5)||(aver[i]>=90))
			{
				printf("%d ",num[i]);
				for(k=0;k<5;k++)
					printf("%.2f ",*(pscore+5*i+k));
				printf("%.2f\n",aver[i]);
			}
	}

}

int main()
{
    void avsco(float *,float *);
    void avcour1(char (*)[10],float *);
    void fail2(char course[5][10],int num[],float *pscore,float aver[4]);
    void good(char course[5][10],int num[4],float *pscore,float aver[4]);
    int i,j,*pnum,num[4];
    float score[4][5],aver[4],*pscore,*paver;
    char course[5][10],(*pcourse)[10];
    pcourse=course;
    for (i=0; i<5; i++)
		scanf("%s",course[i]);
    pscore=&score[0][0];
    pnum=&num[0];
    for (i=0; i<4; i++)
    {        
		scanf("%d",pnum+i);
        for (j=0; j<5; j++)
			scanf("%f",pscore+5*i+j);
    }
    paver=&aver[0];
    avsco(pscore,paver);        
    avcour1(pcourse,pscore);    
    fail2(pcourse,pnum,pscore,paver);  
    good(pcourse,pnum,pscore,paver);   
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注