在歌手大奖赛中,有10个评委为参赛的选手打分,分数为0~10分。选手最后得分为:去掉一个最高分和一个最低分后,取其余8个分数的平均值。
/*
*Copyright (c) 2014,烟台大学计算机学院
*All gight reserved.
*文件名称:temp.cpp
*作者:邵帅
*完成时间:2014年11月2日
*版本号:v1.0
*/
#include<iostream>
using namespace std;
int main()
{
double max,min,score,sum,avr;
int i;
max=0;
min=11;
cout<<"请输入选手的成绩(0-10)"<<endl;
for (i=1; i<10; i++)
{
cout<<"请输入第"<<i<<"评委的给分:";
cin>>score;
if (score>max)
max=score;
if (score<min)
min=score;
sum=sum+score;
}
avr=(sum-max-min)/8;
cout<<"去掉一个最高分:"<<max<<" 去掉一个最低分:"<<min<<endl;
cout<<"当前选手的最后得分是:"<<avr;
return 0;
}
运行结果:
当评委人数并不固定为10人,修改程序,可以选择在运行开始时输入评委人数。
/*
*Copyright (c) 2014,烟台大学计算机学院
*All gight reserved.
*文件名称:temp.cpp
*作者:邵帅
*完成时间:2014年11月2日
*版本号:v1.0
*/
#include<iostream>
using namespace std;
int main()
{
double max,min,score,sum,avr;
int i,x;
max=0;
min=11;
cout<<"请输入评委的人数:";
cin>>x;
cout<<"请输入选手的成绩(0-10)"<<endl;
for (i=1; i<=x; i++)
{
cout<<"请输入第"<<i<<"评委的给分:";
cin>>score;
if (score>max)
max=score;
if (score<min)
min=score;
sum=sum+score;
}
avr=(sum-max-min)/8;
cout<<"去掉一个最高分:"<<max<<" 去掉一个最低分:"<<min<<endl;
cout<<"当前选手的最后得分是:"<<avr;
return 0;
}
运行结果:
输入的成绩必须在0-10之间,当输入错误时要能马上重新输入,直到输入值在正确的范围内。
/*
*Copyright (c) 2014,烟台大学计算机学院
*All gight reserved.
*文件名称:temp.cpp
*作者:邵帅
*完成时间:2014年11月2日
*版本号:v1.0
*/
#include<iostream>
using namespace std;
int main()
{
double max,min,score,sum,avr;
int i,x;
max=0;
min=11;
cout<<"请输入评委的人数:";
cin>>x;
cout<<"请输入选手的成绩(0-10)"<<endl;
i=1;
while(i<=x)
{
cout<<"请输入第"<<i<<"评委的给分:";
cin>>score;
if (score>10||score<0)
continue;
if (score>max)
max=score;
if (score<min)
min=score;
sum=sum+score;
i++;
}
avr=(sum-max-min)/(x-2);
cout<<"去掉一个最高分:"<<max<<" 去掉一个最低分:"<<min<<endl;
cout<<"当前选手的最后得分是:"<<avr;
return 0;
}
运行结果:
一次比赛有好几十位选手参加,输出当前选手的最后得分后,提示“按N键退出,其他键继续...”如果输入的不是N或n,可以为下一位选手计算成绩。
/*
*Copyright (c) 2014,烟台大学计算机学院
*All gight reserved.
*文件名称:temp.cpp
*作者:邵帅
*完成时间:2014年11月2日
*版本号:v1.0
*/
#include<iostream>
using namespace std;
int main()
{
double max,min,score,sum,avr;
int i,x;
char choice;
max=0;
min=11;
while (choice!='N' && choice!='n')
{
cout<<"请输入评委的人数:";
cin>>x;
cout<<"请输入选手的成绩(0-10)"<<endl;
i=1;
while(i<=x)
{
cout<<"请输入第"<<i<<"评委的给分:";
cin>>score;
if (score>10||score<0)
continue;
if (score>max)
max=score;
if (score<min)
min=score;
sum=sum+score;
i++;
}
avr=(sum-max-min)/(x-2);
cout<<"去掉一个最高分:"<<max<<" 去掉一个最低分:"<<min<<endl;
cout<<"当前选手的最后得分是:"<<avr<<endl;
cout<<endl<<"按N退出,其他键继续...";
cin>>choice;
}
return 0;
}
运行结果:
@ Mayuko