题1.
M个歌手,N个评委,对于每个歌手,在评委的打分中,去除一个最高分,去除一个最低分后,按照其余评委的总分降序排名。
要求:利用文件。
帮我看一下
#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");
}