PTA第9章文件作业9-5 谁是冠军 (10分)

                  9-5 谁是冠军 (10分)

文件查重:若干名选手参加扣篮大赛,共有5个评委,现有一个二进制文件pf.dat,内容为选手参加扣篮大赛决赛的成绩(总分高者获胜),编程找出谁是champion(输出冠军者姓名)。
输出格式:如果冠军是zhangwei,则输出zhangwei is champion!
struct person { char name[20]; int score[5]; };
编写的源程序命名为test.cpp,提交之前,将pf.dat和test.cpp放在src文件夹下,然后在src文件夹的上层文件夹中压缩src.zip,保证这个zip文件中有src文件夹和其中的test.cpp文件。
如果使用WinRAR、7z等压缩软件,请注意压缩文件的格式为zip,而不是压缩为其他格式后修改后缀为zip。目前已知使用Windows的资源管理器的右键菜单中的“发送到压缩(zipped)文件夹“功能产生的zip文件不被PTA所接受。Unix的zip命令压缩的文件夹可以接受。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct person 
{
    
    	char name[20]; 
	int score[5]; 
};
int main()
{
    
    
	struct person st[100];
	int i = 0,j,sum,su[100];
	char s[20];
	FILE *fp1;
	if((fp1=fopen("src/pf.dat","rb"))==NULL)
	{
    
    
		printf("can't open this file");
	}
	while(fread(&st[i],sizeof(struct person ),1,fp1)==1)
	{
    
    
		
		su[i] = st[i].score[0]+st[i].score[1]+st[i].score[2]+st[i].score[3]+st[i].score[4];
	//	printf("%s %d %d %d %d %d %d\n",st[i].name,st[i].score[0],st[i].score[1],st[i].score[2],st[i].score[3],st[i].score[4],su[i]);
		i++;
		
	}
	sum=su[0];
	strcpy(s,st[0].name);
//	printf("%s\n",s);
	for(j=0;j<i;j++)
	{
    
    
		if(su[j]>sum)
		{
    
    
			sum=su[j];
			strcpy(s,st[j].name);			
		}
	}
	printf("%s is champion!",c);	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/csx_zzh/article/details/106295738