某学校的期末考试共有n个学生参加,考试科目共有m科。学校将会给一部分学生颁发单科成绩优秀奖,获奖学生需要满足的条件是某一科的成绩是所有学生中最高的或是最高的之一。请问学校应该给多少名学生颁发单科成绩优秀奖,用java代码实现
时间: 2023-08-31 21:32:05 浏览: 112
### 回答1:
根据题意,我们需要找到每一科的最高分,然后再统计有多少个学生的成绩是最高分或最高分之一。
Java代码实现如下:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(); // 学生人数
int m = scanner.nextInt(); // 科目数
int[][] scores = new int[n][m]; // 存储每个学生的成绩
int[] maxScores = new int[m]; // 存储每一科的最高分
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scores[i][j] = scanner.nextInt();
if (scores[i][j] > maxScores[j]) {
maxScores[j] = scores[i][j];
}
}
}
int count = 0; // 统计获奖学生人数
for (int i = 0; i < n; i++) {
boolean isMax = false;
for (int j = 0; j < m; j++) {
if (scores[i][j] == maxScores[j]) {
isMax = true;
break;
}
}
if (isMax) {
count++;
}
}
System.out.println(count);
}
}
```
输入样例:
```
5 3
90 80 70
80 70 60
85 90 95
95 95 95
80 75 70
```
输出样例:
```
3
```
### 回答2:
假设有一个 int[][] scores 数组来保存所有学生的成绩,其中 scores[i][j] 表示第 i 个学生在第 j 门科目的成绩。
```java
public static void main(String[] args) {
int n = 10; // 学生数量
int m = 5; // 科目数量
int[][] scores = new int[n][m]; // 学生成绩数组
// 随机生成学生成绩
Random random = new Random();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scores[i][j] = random.nextInt(101);
}
}
int[] maxScores = new int[m]; // 每门科目的最高分
for (int j = 0; j < m; j++) {
int maxScore = 0;
for (int i = 0; i < n; i++) {
if (scores[i][j] > maxScore) {
maxScore = scores[i][j];
}
}
maxScores[j] = maxScore;
}
int count = 0; // 优秀奖获奖人数
for (int i = 0; i < n; i++) {
boolean hasMaxScore = false;
for (int j = 0; j < m; j++) {
if (scores[i][j] == maxScores[j]) {
hasMaxScore = true;
break;
}
}
if (hasMaxScore) {
count++;
}
}
System.out.println("应该给 " + count + " 名学生颁发单科成绩优秀奖");
}
```
以上代码首先生成了一个 n 行 m 列的二维数组 scores,随机生成学生成绩。然后遍历每门科目,找出每门科目的最高分,存储在 maxScores 数组中。接着遍历每个学生,判断他们是否满足任何一门成绩是最高分,如果是,则 count 加一。最后输出优秀奖获奖人数 count。