华为2012秋季校招机试题-选秀节目打分(题目来自网络)
选秀节目打分选秀节目打分,分为专家评委和大众评委,score[] 数组里面存储每个评委打的分数,judge_type[] 里存储与 score[] 数组对应的评委类别,judge_type == 1,表示专家评委,judge_type == 2,表示大众评委,n表示评委总数。
打分规则如下:专家评委和大众评委的分数先分别取一个平均分(平均分取整),然后,总分 = 专家评委平均分 * 0.6 + 大众评委 * 0.4,总分取整。
如果没有大众评委,则 总分 = 专家评委平均分,总分取整。函数最终返回选手得分。
样例:
输入:
90 80 87 89 91
1 2 1 1 1
输出:
85
思路:直接对等级数组进行遍历,将专家和大众区分开;之后判定是否全是专家,进而进行相应的处理。
注:题目中并未给出取整的具体如何取整,程序中使用了四舍五入。
package com.liuhao;
import java.util.Scanner;
public class Score {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
final int N = 5;
int[] score = new int;
int[] judge_type = new int;
for (int i = 0; i < score.length; i++) {
score = scan.nextInt();
}
// judge_type == 1,表示专家评委,judge_type == 2,表示大众评委
for (int i = 0; i < judge_type.length; i++) {
judge_type = scan.nextInt();
}
// 总分 = 专家评委平均分 * 0.6 + 大众评委 * 0.4,总分取整。
// 如果没有大众评委,则 总分 = 专家评委平均分,总分取整。
double sum = 0; //存放最后的分数
int expNum = 0; //存放专家人数
int dazNum = 0; //存放大众人数
double expSum = 0; //存放专家总分数
double dazSum = 0; //存放大众总分数
//分别统计专家和大众的总人数和总分数
for (int i = 0; i < judge_type.length; i++) {
if (1 == judge_type) {
expNum++;
expSum += score;
}
if (2 == judge_type) {
dazNum++;
dazSum += score;
}
}
//最后的分数统计
//首先判断是否全是专家
//是,则 总分 = 专家评委平均分
if (isAllExpert(judge_type)) {
sum = expSum / expNum;
}
//否,则 总分 = 专家评委平均分 * 0.6 + 大众评委 * 0.4,总分取整。
else {
sum = Math.round(expSum / expNum) * 0.6 + Math.round(dazSum / dazNum) * 0.4;
}
//总分取整。
System.out.println(Math.round(sum));
}
private static boolean isAllExpert(int[] a) {
boolean temp = true;
for (int i = 0; i < a.length; i++) {
if (2 == a) {
temp = false;
break;
}
}
return temp;
}
}
页:
[1]