构造一个成绩管理系统CourseManagementSystem,该系统包括如下几个方法:void add(int no, int grade)添加该学号的成绩,如果系统已有该学生成绩,则输出"the student already exists";void delete(int no)删除某学号成绩,如果不存在此学生则输出"no such student";int query(int no)查询并返回该学号的成绩;统计成绩void statistics( )统计[0-59]、[60-69]、[70-79]、[80-89]、[90-100]各成绩段的学生个数并打印。请选择合适的容器实现上述功能。(题目假设不会重复添加相同学号的学生成绩)
main函数中读入操作类型及相关参数,并调用statictic函数输出学生成绩统计信息。
输入描述:
操作个数
操作名 操作参数
输出描述:
查询学生的成绩
各成绩段的学生个数
裁判测试程序样例:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
CourseManagementSystem cms = new CourseManagementSystem();
int ops = sc.nextInt();
for (int i=0;i<ops;i++) {
String op = sc.next();
if (op.equals("add"))
cms.add(sc.nextInt(), sc.nextInt());
else if (op.equals("delete"))
cms.delete(sc.nextInt());
else if (op.equals("query")) {
int no = sc.nextInt();
int s = cms.query(no);
System.out.println("the score for "+no+" is : "+s);
}
}
cms.statistic();
}
}
/* 你的代码被嵌在这里*/
输入样例:
在这里给出一组输入。例如:
8
add 1 63
add 2 78
add 3 74
delete 3
add 2 20
delete 5
query 1
add 4 90
输出样例:
在这里给出相应的输出。例如:
the student already exists
no such student
the score for 1 is : 63
[0-59] : 0
[60-69] : 1
[70-79] : 1
[80-89] : 0
[90-100] : 1
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
AC代码
class CourseManagementSystem {
int[] no = new int[1000];
int[] grade = new int[1000];
int count = 0;
int index;
void add(int no, int grade) {
boolean flag = true;
for (int i = 0; i < count; i++) {
if (this.no[i] == no) {
flag = false;
System.out.println("the student already exists");
break;
}
}
if (flag) {
this.no[count] = no;
this.grade[count] = grade;
count++;
}
}
void delete(int no) {
boolean found = false;
for (int i = 0; i < count; i++) {
if (this.no[i] == no) {
index = i;
found = true;
break;
}
}
if (found) {
for (int i = index; i < count - 1; i++) {
this.no[i] = this.no[i + 1];
this.grade[i] = this.grade[i + 1];
}
count--;
} else {
System.out.println("no such student");
}
}
int query(int no) {
int z = -1;
for (int i = 0; i < count; i++) {
if (this.no[i] == no) {
z = i;
break;
}
}
return z != -1 ? grade[z] : -1;
}
void statistic() {
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
for (int i = 0; i < count; i++) {
if (grade[i] >= 0 && grade[i] <= 59)
count1++;
else if (grade[i] >= 60 && grade[i] <= 69)
count2++;
else if (grade[i] >= 70 && grade[i] <= 79)
count3++;
else if (grade[i] >= 80 && grade[i] <= 89)
count4++;
else if (grade[i] >= 90 && grade[i] <= 100)
count5++;
}
System.out.println("[0-59] : " + count1);
System.out.println("[60-69] : " + count2);
System.out.println("[70-79] : " + count3);
System.out.println("[80-89] : " + count4);
System.out.println("[90-100] : " + count5);
}
}