题1.
M个歌手,N个评委,对于每个歌手,在评委的打分中,去除一个最高分,去除一个最低分后,按照其余评委的总分降序排名。
要求:利用文件。
#include <stdio.h>
#include <conio.h>
#define SINGER_NUM 5 /*歌手个数*/
#define JUDGE_NUM 5 /*评委个数*/
typedef struct
{
int score; /*分数*/
int num; /*歌手编号*/
}SINGER_Struct;
int getScore(int JUDGE_Score[]); /*给出每个评委的分数得到歌手的分数*/
int main(void)
{
SINGER_Struct SINGER[SINGER_NUM]; /*每个运动员得分*/
SINGER_Struct temp;
int JUDGE_Score[JUDGE_NUM]; /*每个评委给出的分数*/
int i;
int j;
FILE *pFile=NULL; /*声明一个文件指针*/
clrscr(); /*清屏*/
/*分别给每个歌手给分*/
for(i=0;i<SINGER_NUM;i++)
{
printf("请给歌手 %d 给分\n",i+1);
SINGER[i].num=i+1; /*歌手号是从1开始*/
for(j=0;j<5;j++) /*每个评委对歌手给分*/
scanf("%d",&JUDGE_Score[j]);
SINGER[i].score=getScore(JUDGE_Score);/*计算出歌手的得分*/
}
/*利用冒泡排序算法对歌手分数排序*/
for(i=SINGER_NUM-1;i>0;i--)
{
for(j=0;j<i;j++)
{
if(SINGER[j].score<SINGER[j+1].score)
{
temp=SINGER[j];
SINGER[j]=SINGER[j+1];
SINGER[j+1]=temp;
}
}
}
/*打开一个文件,并把歌手从最高分到最低分写入文件*/
pFile=fopen("SINGER.txt","w");
for(i=0;i<SINGER_NUM;i++)
{
fprintf(pFile,"歌手%d 的分数: %d\n",SINGER[i].num,SINGER[i].score);
}
/*暂停*/
getch();
/*关闭文件*/
fclose(pFile);
return 0;
}
int getScore(int JUDGE_Score[])
{
int total=0;
int min=JUDGE_Score[0],max=JUDGE_Score[0];
int i;
for(i=0;i<JUDGE_NUM;i++)
{
total+=JUDGE_Score[i];
if(min>JUDGE_Score[i])
min=JUDGE_Score[i];
if(max<JUDGE_Score[i])
max=JUDGE_Score[i];
}
return (total-min-max);
}
/*刚好有时间,把没有做的代码,写一下*/
帮我看一下
#include <iostream>
#include <fstream>
using namespace std;
typedef struct singer
{
double score;
int index;
}singer;
double sor(double *pn,int n,double sum)
{
double max,min;
max=min=pn[0];
for(int i=1;i<n;i++)
{
if(max<pn[i])
max=pn[i];
if(min>pn[i])
min=pn[i];
}
sum=sum-max-min;
return sum;
}
int swap(singer *a,singer *b)
{
singer temp=*a;
*a=*b;
*b=temp;
return 1;
}
void main()
{
int m,n,i,temp;
double sum=0;
cout<<"输入歌手数目"<<endl;
cin>>m;
cout<<"输入裁判数目"<<endl;
cin>>n;
singer *pm=new singer[m];
double *pn=new double[n];
for(i=0;i<m;i++)
{
cout<<"请给"<<i+1<<"号歌手打分"<<endl;
for(int j=0;j<n;j++)
{
cout<<j+1<<"号裁判打分为:"<<endl;
cin>>pn[j];
sum+=pn[j];
}
temp=sor(pn,n,sum);
sum=0;
pm[i].score=temp;
pm[i].index=i;
}
for(i=0;i<m;i++)
{
cout<<pm[i].index+1<<"号歌手的总分是:"<<pm[i].score<<endl;
}
for(i=1;i<m;i++)
{
for(int j=0;j<m-i;j++)
{
if(pm[j].score<pm[j+1].score)
{
swap(pm[j],pm[j+1]);
}
}
cout<<"结果如下:"<<endl;
for(i=0;i<m;i++)
{
cout<<"第"<<i+1<<"名为: "<<pm[i].index+1<<"号歌手,"<<
"去掉一个最高分,去掉一个最低分,他的总分是: "<<pm[i].score<<endl;
}
ofstream outfile("sing.txt");
for(i=0;i<m;i++)
{
outfile<<"第"<<i+1<<"名为: "<<pm[i].index+1<<"号歌手,"<<
"去掉一个最高分,去掉一个最低分,他的总分是: "<<pm[i].score<<endl;
}
outfile.close();
cout<<"上述结果已成功写入文件sing.txt中"<<endl;
system("PAUSE");
}