2023華為OD統(tǒng)一考試(A+B卷)題庫(kù)清單-帶答案(持續(xù)更新)or2023年華為OD真題機(jī)考題庫(kù)大全-帶答案(持續(xù)更新)
項(xiàng)目描述
現(xiàn)有兩門選修課,每門選修課都有一部分學(xué)生選修,每個(gè)學(xué)生都有選修課的成績(jī),需要你找出同時(shí)選修了兩門選修課的學(xué)生,先按照班級(jí)進(jìn)行劃分,班級(jí)編號(hào)小的先輸出,每個(gè)班級(jí)按照兩門選修課成績(jī)和的降序排序,成績(jī)相同時(shí)按照學(xué)生的學(xué)號(hào)升序排序。
輸入描述
第一行為第一門選修課學(xué)生的成績(jī)
第二行為第二門選修課學(xué)生的成績(jī),每行數(shù)據(jù)中學(xué)生之間以英文分號(hào)分隔,每個(gè)學(xué)生的學(xué)號(hào)和成績(jī)以英文逗號(hào)分隔,學(xué)生學(xué)號(hào)的格式為8位數(shù)字(2位院系編號(hào)+入學(xué)年份后2位+院系內(nèi)部1位專業(yè)編號(hào)+所在班級(jí)3位學(xué)號(hào)),學(xué)生成績(jī)的取值范圍為[0,100]之間的整數(shù),兩門選修課選修學(xué)生數(shù)的取值范圍為[1-2000]之間的整數(shù)。
輸出描述
同時(shí)選修了兩門選修課的學(xué)生的學(xué)號(hào),如果沒有同時(shí)選修兩門選修課的學(xué)生輸出NULL,否則,先按照班級(jí)劃分,班級(jí)編號(hào)小的先輸出,每個(gè)班級(jí)先輸出班級(jí)編號(hào)(學(xué)號(hào)前五位),然后另起一行輸出這個(gè)班級(jí)同時(shí)選修兩門選修課的學(xué)生學(xué)號(hào),學(xué)號(hào)按照要求排序(按照兩門選修課成績(jī)和的降序,成績(jī)和相同時(shí)按照學(xué)號(hào)升序),學(xué)生之間以英文分號(hào)分隔。
示例1
輸入:
01202021,75;01201033,95;01202008,80;01203006,90;01203088,100
01202008,70;01203088,85;01202111,80;01202021,75;01201100,88
輸出:
01202
01202008;01202021
01203
01203088
說(shuō)明:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-624001.html
同時(shí)選修了兩門選修課的學(xué)生01202021、01202008、01203088,這三個(gè)學(xué)生兩門選修課的成績(jī)和分別為150、150、185, 01202021、01202008屬于01202班的學(xué)生,按照成績(jī)和降序,成績(jī)相同時(shí)按學(xué)號(hào)升序輸出的結(jié)果為01202008:01202021,01203088屬于01203班的學(xué)生,按照成績(jī)和降序,成績(jī)相同時(shí)按學(xué)號(hào)升序輸出的結(jié)果為01203088,01202的班級(jí)編號(hào)小于01203的班級(jí)編號(hào),需要先輸出。
示例2
輸入:
01201022,75;01202033,95;01202018,80;01203006,90;01202066,100
01202008,70;01203102,85;01202111,80;01201021,75;01201100,88
輸出:
NULL
說(shuō)明:
沒有同時(shí)選修了兩門選修課的學(xué)生,輸出NULL。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-624001.html
public class OptionalCourse { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] one = sc.nextLine().split(";"); String [] two = sc.nextLine().split(";"); student(one,two); } public static void student(String[] one,String [] two){ List<StudentInfo> oneStudentInfos = new ArrayList<>(); List<StudentInfo> twoStudentInfos = new ArrayList<>(); List<StudentInfo> endStudentInfos = new ArrayList<>(); Set<String> schoolInfo = new HashSet<>(); //2門成績(jī)的id跟分?jǐn)?shù)都存在List對(duì)象中 for (String s : one){ String [] one1 = s.split(","); StudentInfo st = new StudentInfo(one1[0],Integer.valueOf(one1[1])); oneStudentInfos.add(st); } for (String s : two){ String [] two2 = s.split(","); StudentInfo st = new StudentInfo(two2[0],Integer.valueOf(two2[1])); twoStudentInfos.add(st); } //將2門都存在成績(jī)的學(xué)生信息統(tǒng)計(jì) for (int i = 0; i < oneStudentInfos.size(); i++){ for (int j = 0; j < twoStudentInfos.size(); j++){ if (oneStudentInfos.get(i).id.equals(twoStudentInfos.get(j).id)){ int score = oneStudentInfos.get(i).score + twoStudentInfos.get(i).score; StudentInfo st = new StudentInfo(oneStudentInfos.get(i).id,score); endStudentInfos.add(st); //截取開頭5位數(shù) String schoolId = oneStudentInfos.get(i).id.substring(0,5); schoolInfo.add(schoolId); } } } if (endStudentInfos.size() == 0){ System.out.println("NULL"); return; } //將set開頭5位數(shù)排序后存在List中 List<String> schoolInfos = schoolInfo.stream() .sorted(Comparator.naturalOrder()) .collect(Collectors.toList()); compareStudent(endStudentInfos,schoolInfos); } /** * 按照存儲(chǔ)的開頭5位數(shù)進(jìn)行分類輸出 * @param endStudentInfos * @param schoolInfo */ public static void compareStudent(List<StudentInfo> endStudentInfos,List<String> schoolInfo){ Collections.sort(endStudentInfos,new StudentInfo()); StringBuffer sb = new StringBuffer(); for (int i = 0; i < schoolInfo.size();i++){ System.out.println(schoolInfo.get(i)); for (int j = 0; j < endStudentInfos.size(); j++){ if (endStudentInfos.get(j).id.substring(0,5).equals(schoolInfo.get(i))){ sb.append(endStudentInfos.get(j).id).append(";"); } } System.out.println(sb.toString().substring(0,sb.length()-1)); sb.setLength(0); } } @Data static class StudentInfo implements Comparator<StudentInfo> { String id; int score; public StudentInfo(String id, int score) { this.id = id; this.score = score; } public StudentInfo() { } @Override public int compare(StudentInfo o1, StudentInfo o2) { return Integer.valueOf(o1.id) - Integer.valueOf(o2.id); } } }
到了這里,關(guān)于華為OD真題--選修課--帶答案的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!