关于计算机网站建设的论文,灌南县建设局网站,织梦本地做网站,软件开发培训课程咨询题目描述
小明来到某学校当老师#xff0c;需要将学生按考试总分或单科分数进行排名。输入包括学生人数、科目数量、科目名称、每个学生的姓名和对应科目的成绩#xff0c;最后输入一个用作排名的科目名称。如果输入的排名科目不存在#xff0c;则按总分进行排序。输出一行…题目描述
小明来到某学校当老师需要将学生按考试总分或单科分数进行排名。输入包括学生人数、科目数量、科目名称、每个学生的姓名和对应科目的成绩最后输入一个用作排名的科目名称。如果输入的排名科目不存在则按总分进行排序。输出一行按成绩排序后的学生名字名字之间用空格隔开。成绩相同的按照学生姓名字典顺序排序。
输入描述
第1行输入两个整数分别表示学生人数n和科目数量m。第2行输入m个科目名称彼此之间用空格隔开。接下来的n行每行包含一个学生的姓名和该生m个科目的成绩空格隔开。第n2行输入用作排名的科目名称。
输出描述
输出一行按成绩排序后的学生名字名字之间用空格隔开。
示例
示例1
输入
3 2
yuwen shuxue
fangfang 95 90
xiaohua 88 95
minmin 100 82
shuxue输出
xiaohua fangfang minmin说明按shuxue成绩排名依次是xiaohua、fangfang、minmin。
示例2
输入
3 2
yuwen shuxue
fangfang 95 90
xiaohua 88 95
minmin 90 95
zongfen输出
fangfang minmin xiaohua说明排序科目不存在按总分排序fangfang和minmin总分相同按姓名的字典顺序fangfang排在前面。
解题思路
读取输入首先读取学生人数、科目数量、科目名称、每个学生的姓名和对应科目的成绩以及用作排名的科目名称。处理数据 创建一个学生数据结构如结构体或类包含学生姓名、各科成绩和总分。读取数据时计算每个学生的总分并存储在相应的数据结构中。 确定排序规则 检查用作排名的科目名称是否存在于输入的科目列表中。如果存在则按该科目成绩进行排序如果不存在则按总分进行排序。 排序使用合适的排序算法如快速排序、归并排序等对学生数组进行排序。如果成绩相同则按姓名的字典顺序排序。输出按排序后的顺序输出学生的名字。
注意事项
在处理输入时要注意数据的有效性和边界情况如输入数据的格式是否正确、是否有非法字符等。在计算总分和排序时要注意整数溢出的问题。在编写代码时要注意代码的健壮性和可读性尽量使用简洁明了的代码结构。
代码实现 import lombok.Data;
import lombok.Getter;import java.io.Serializable;Data
public class Student implements Serializable {// 获取学生姓名的方法Getterprivate String name;private int[] scores;// 假设有一个构造函数来初始化这些字段public Student(String name, int[] scores) {this.name name;this.scores scores;}// 获取科目成绩的方法public int getScores(int index) {return scores[index];}// 如果需要可以添加计算总分的方法public int getTotalScore() {int total 0;for (int score : scores) {total score;}return total;}public Student getStudent() {return new Student(name, scores);}
}import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;public class SmartGradeSheet {public static void main(String[] args) {Scanner scanner new Scanner(System.in);int n scanner.nextInt(); // 学生人数int m scanner.nextInt(); // 科目数量scanner.nextLine(); // 跳过行尾String[] subjects scanner.nextLine().split(\\s); // 读取科目名称ListStudent students new ArrayList();for (int i 0; i n; i) {String[] inputs scanner.nextLine().split(\\s);String name inputs[0];int[] scores new int[m];for (int j 1; j m; j) {scores[j - 1] Integer.parseInt(inputs[j]);}students.add(new Student(name, scores));}String rankingSubject scanner.nextLine(); // 读取用作排名的科目名称ComparatorStudent comparator null;if (contains(subjects, rankingSubject)) {// 按指定科目成绩排序int subjectIndex getIndex(subjects, rankingSubject);comparator Comparator.comparingInt( (**Student student**) - student.getScores(subjectIndex)).thenComparing(Student::getName, Comparator.naturalOrder());} else {// 按总分排序comparator Comparator.comparingInt(Student::getTotalScore).thenComparing(Student::getName);}students.sort(comparator);// 输出结果for (int i 0; i students.size(); i) {if (i 0) System.out.print( );System.out.print(students.get(i).getName());}System.out.println();scanner.close();}// 辅助方法检查科目名称是否存在于数组中private static boolean contains(String[] array, String target) {for (String item : array) {if (item.equals(target)) {return true;}}return false;}// 辅助方法获取科目名称在数组中的索引private static int getIndex(String[] array, String target) {for (int i 0; i array.length; i) {if (array[i].equals(target)) {return i;}}throw new IllegalArgumentException(Subject not found: target);}// 注意这里需要给Student类添加getTotalScore()方法但为了简洁我直接在构造函数中计算了总分// 如果需要可以添加如下方法到Student类中// public int getTotalScore() {// return totalScore;// }}记一下写代码过程中提示的报错
最先
comparator Comparator.comparingInt( student - student.getScores(subjectIndex))SmartGradeSheet.java:41:68 java: 找不到符号 符号: 方法 getScores(int) 位置: 类型为java.lang.Object的变量 student。 网上查了说这种是因为 Student类缺少了getScores(int subjectIndex方法但实际是有的。 继续排查说Comparator 中的 student 被隐式地当作 java.lang.Object 类型处理而不是 Student 类型但实际早就定义为了ComparatorStudent所以问题不在这里。 最后发现 应该使用 (Student student) - student.getScores(subjectIndex) 明确指定 student 的类型为 Student 为什么要这样使用呢
在 Java 中Lambda 表达式的参数类型可以通过类型推断来确定。然而在某些情况下类型推断可能会出现问题导致编译器无法正确识别参数类型。以下是一些具体的原因和解释
类型推断问题 当编译器无法准确推断 Lambda 参数的类型时需要显式指定类型。例如在以下情况下编译器可能无法正确推断类型 Comparator comparator (student) - student.getScores(subjectIndex); 如果编译器无法从上下文中推断出 student 的类型就会将其视为 Object 类型从而导致找不到 getScores 方法。显式类型声明提高代码可读性和可维护性 显式指定类型可以提高代码的可读性和可维护性。例如 Comparator comparator (Student student) - student.getScores(subjectIndex); 文章转载自: http://www.morning.yjdql.cn.gov.cn.yjdql.cn http://www.morning.mjwnc.cn.gov.cn.mjwnc.cn http://www.morning.lwnb.cn.gov.cn.lwnb.cn http://www.morning.zmzdx.cn.gov.cn.zmzdx.cn http://www.morning.jcfqg.cn.gov.cn.jcfqg.cn http://www.morning.wrfk.cn.gov.cn.wrfk.cn http://www.morning.kkysz.cn.gov.cn.kkysz.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.hxcrd.cn.gov.cn.hxcrd.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.nbgfk.cn.gov.cn.nbgfk.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.hwlk.cn.gov.cn.hwlk.cn http://www.morning.fxxmj.cn.gov.cn.fxxmj.cn http://www.morning.xcbnc.cn.gov.cn.xcbnc.cn http://www.morning.fhjnh.cn.gov.cn.fhjnh.cn http://www.morning.tpwrm.cn.gov.cn.tpwrm.cn http://www.morning.xltdh.cn.gov.cn.xltdh.cn http://www.morning.xjqhh.cn.gov.cn.xjqhh.cn http://www.morning.tmfhx.cn.gov.cn.tmfhx.cn http://www.morning.sgbk.cn.gov.cn.sgbk.cn http://www.morning.znpyw.cn.gov.cn.znpyw.cn http://www.morning.xlndf.cn.gov.cn.xlndf.cn http://www.morning.xcfmh.cn.gov.cn.xcfmh.cn http://www.morning.kpfds.cn.gov.cn.kpfds.cn http://www.morning.ymbqr.cn.gov.cn.ymbqr.cn http://www.morning.cnvlog.cn.gov.cn.cnvlog.cn http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.qsmdd.cn.gov.cn.qsmdd.cn http://www.morning.rwzc.cn.gov.cn.rwzc.cn http://www.morning.swdnr.cn.gov.cn.swdnr.cn http://www.morning.mzrqj.cn.gov.cn.mzrqj.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.gxfpk.cn.gov.cn.gxfpk.cn http://www.morning.dhqzc.cn.gov.cn.dhqzc.cn http://www.morning.wxgd.cn.gov.cn.wxgd.cn http://www.morning.dhbyj.cn.gov.cn.dhbyj.cn http://www.morning.srwny.cn.gov.cn.srwny.cn http://www.morning.lekbiao.com.gov.cn.lekbiao.com http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn http://www.morning.fyxr.cn.gov.cn.fyxr.cn http://www.morning.bqrd.cn.gov.cn.bqrd.cn http://www.morning.gfhng.cn.gov.cn.gfhng.cn http://www.morning.cqrenli.com.gov.cn.cqrenli.com http://www.morning.sjqpm.cn.gov.cn.sjqpm.cn http://www.morning.rbbgh.cn.gov.cn.rbbgh.cn http://www.morning.wqcz.cn.gov.cn.wqcz.cn http://www.morning.rbcw.cn.gov.cn.rbcw.cn http://www.morning.tbjb.cn.gov.cn.tbjb.cn http://www.morning.kxrld.cn.gov.cn.kxrld.cn http://www.morning.lwtfx.cn.gov.cn.lwtfx.cn http://www.morning.xxhc.cn.gov.cn.xxhc.cn http://www.morning.hfbtt.cn.gov.cn.hfbtt.cn http://www.morning.dblgm.cn.gov.cn.dblgm.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.xjpnq.cn.gov.cn.xjpnq.cn http://www.morning.clnmf.cn.gov.cn.clnmf.cn http://www.morning.cytr.cn.gov.cn.cytr.cn http://www.morning.brbmf.cn.gov.cn.brbmf.cn http://www.morning.bmyrl.cn.gov.cn.bmyrl.cn http://www.morning.pqkrh.cn.gov.cn.pqkrh.cn http://www.morning.rnqnp.cn.gov.cn.rnqnp.cn http://www.morning.xmwdt.cn.gov.cn.xmwdt.cn http://www.morning.jjpk.cn.gov.cn.jjpk.cn http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn http://www.morning.srndk.cn.gov.cn.srndk.cn http://www.morning.hfbtt.cn.gov.cn.hfbtt.cn http://www.morning.trwkz.cn.gov.cn.trwkz.cn http://www.morning.yhjrc.cn.gov.cn.yhjrc.cn http://www.morning.mhrzd.cn.gov.cn.mhrzd.cn http://www.morning.nqlx.cn.gov.cn.nqlx.cn http://www.morning.hxftm.cn.gov.cn.hxftm.cn http://www.morning.nqgds.cn.gov.cn.nqgds.cn http://www.morning.trnhy.cn.gov.cn.trnhy.cn http://www.morning.qqnp.cn.gov.cn.qqnp.cn http://www.morning.zmbzl.cn.gov.cn.zmbzl.cn http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn http://www.morning.sqtsl.cn.gov.cn.sqtsl.cn http://www.morning.mgwpy.cn.gov.cn.mgwpy.cn http://www.morning.bauul.com.gov.cn.bauul.com