这篇文章讲述的是算法趣味分数部分的歌星大赛奖问题的java实现,参考的书籍为清华大学出版社出版,贾蓓等编著的《c语言趣味编程1000例》,如有错误或者不当之处,还望各位大神批评指正。
问题描述
在歌星大赛中,有10个评委为参赛的选手打分,分数为1~100分。选手的最后得分为:去掉一个最高分和一个最低分后其余8个分数的平均值。请编写一个程序实现。
算法分析
使用一个数组存放分数,遍历取出最大值与最小值,再计算出平均分即可
代码实现
package fraction;
/**
*
* @author 叶清逸
* @date 2018年7月18日下午10:33:59
* @version 1.0
* @project fraction
*/
public class Q3_StarsScore {
/**
* 算法描述:在歌星大赛中,有10个评委为参赛的选手打分,分数为1~100分。选手的最后得分为:去掉一个最高分和一个最低分
* 后其余8个分数的平均值。请编写一个程序实现。
*
* 算法分析:使用一个数组存放分数,遍历取出最大值与最小值,再计算出平均分即可
*/
public static void main(String[] args) {
/*存放评委打出的分数*/
float scores [] = new float[]{46,47,98,87,77,69,43,56,38,80} ;
System.out.print("分数:");
for(int i=0 ; i<scores.length ; i++){
System.out.print(scores[i]+" ");
}
System.out.println();
/*记录最大值与最小值的索引*/
int min = 0 ;
int max = 0 ;
for(int i=0 ; i<scores.length ;i++){
if(scores[i] > scores[max]) //记录最大值的索引
max = i ;
if(scores[i] < scores[min]) //记录最小值的索引
min = i ;
}
/*将最大最小位上的值制为空*/
scores[max] = 0 ;
scores[min] = 0 ;
/*计算平均分*/
float sum = 0 ;
for(int j=0 ; j<scores.length ; j++){
sum += scores[j] ;
}
float avg = sum/8 ;
/*输出结果*/
System.out.println("选手最后得分为:"+avg);
}
}
样例输出
分数:46.0 47.0 98.0 87.0 77.0 69.0 43.0 56.0 38.0 80.0
选手最后得分为:63.125