Student List for Course (25) cid=100000596

本文介绍了一个课程注册系统的实现方法,该系统能够处理大量学生选课数据,并按课程编号输出选择每门课程的学生名单及其数量。

题目描述:

Zhejiang University has 40000 students and provides 2500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses.

输入:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (<=40000), the total number of students, and K (<=2500), the total number of courses. Then N lines follow, each contains a student's name (3 capital English letters plus a one-digit number), a positive number C (<=20) which is the number of courses that this student has registered, and then followed by C course numbers. For the sake of simplicity, the courses are numbered from 1 to K. 

输出:

For each test case, print the student name lists of all the courses in increasing order of the course numbers. For each course, first print in one line the course number and the number of registered students, separated by a space. Then output the students' names in alphabetical order. Each name occupies a line. 

样例输入:

10 5
ZOE1 2 4 5
ANN0 3 5 2 1
BOB5 5 3 4 2 1 5
JOE4 1 2
JAY9 4 1 2 5 4
FRA8 3 4 2 5
DON2 2 4 5
AMY7 1 5
KAT3 3 5 4 2
LOR6 4 2 4 1 5

样例输出: 

 

1 4
ANN0
BOB5
JAY9
LOR6
2 7
ANN0
BOB5
FRA8
JAY9
JOE4
KAT3
LOR6
3 1
BOB5
4 7
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1
5 9
AMY7
ANN0
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1

思路:

使用一个vector<string> course_list[课程数量]来保存数据,其中第一维代表课程,第二维度存储选了这门课的学生姓名。

第一纬度下标-1就是课程号,例如保存选了1号课程学生姓名的vector可以用course_list[0]表示。在读取所有数据后,对每个第二维vector用sort进行排序然后输出。

我的解决方案: 

#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n,k,c,cur_course;
    string name;
    scanf("%d %d",&n,&k);
    vector<string> course_list[k];
    //course_list下标-1是课程号
    for(int i=0;i<n;i++) {
        cin >> name;
        cin >> c;
        for (int i = 0; i < c; i++) {
            scanf("%d", &cur_course);
            course_list[cur_course - 1].push_back(name);
        }
    }
    for(int i=0;i<k;i++){
        sort(course_list[i].begin(),course_list[i].end());
    }
    for(int i=0;i<k;i++){
        cout<<i+1<<' '<<course_list[i].size()<<endl;
        for(int j=0;j<course_list[i].size();j++){
            cout<<course_list[i][j]<<endl;
        }
    }

return 0;
}

 

using com.znsd; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace znsd_08_方法_班级系统 { internal class Program { static void Main(string[] args) { //容器 string[,] classDB = new string[20, 2]; string[,] studentDB = new string[50, 4]; int studentCount = 0; //计数器 int classCount = 0; //框架 结构 while (true) { Console.WriteLine("1.班级管理 2 学生管理 3 exit"); int mark = Znsd.GetInt(); if (mark == 1) { classCount = ClassManage(classDB,classCount, studentDB, studentCount); } else if (mark == 2) { studentCount = StudentManage(classDB, classCount,studentDB,studentCount); } } } public static int ClassManage(string[,] classDB, int classCount, string[,] studentDB, int studentCount) { while (true) { Console.WriteLine("班级 1 增加 2 删除 3 修改 4 查询"); int mark = Znsd.GetInt(); if (mark == 1) { classCount = ClassInsert(classDB, classCount); } else if (mark == 2) { classCount = ClassDelete(classDB, classCount, studentDB, studentCount); } else if (mark == 3) { ClassUpdate(classDB, classCount, studentDB, studentCount); } else if (mark == 4) { ClassSelect(classDB, classCount); } else if (mark == 5) { break; } } return classCount; } public static int StudentManage(string[,] classDB, int classCount, string[,] studentDB, int studentCount) { while (true) { Console.WriteLine("学生 1 增加 2 删除 3 修改 4 查询"); int mark = Znsd.GetInt(); if (mark == 1) { studentCount = StudentInsert(classDB, classCount, studentDB, studentCount); } else if (mark == 2) { studentCount = StudentDelete( studentDB, studentCount); } else if (mark == 3) { StudentUpdate(studentDB, studentCount); } else if (mark == 4) { StudentSelect(classDB, classCount, studentDB, studentCount); } else if (mark == 5) { break; } } return studentCount; } public static int ClassInsert(string[,] classDB, int classCount) { //增加逻辑 Console.WriteLine("请输入班级编号:"); string id = Znsd.GetString(); //校验 //思路:拿着id到数组里挨个去找是否有相同的 int index = -1; for (int i = 0; i < classCount; i++) { //for就能够将数组里的每一个都拿出来stuDB[i,0] if (classDB[i, 0].Equals(id)) { index = i;//把行号 赋值给index 0 1 2 3 4 } } if (index == -1)//没有重复的 { Console.WriteLine("请输入班级描述:"); string desc = Znsd.GetString(); //将数据放入到容器 classDB[classCount, 0] = id; classDB[classCount, 1] = desc; classCount++; } else { Console.WriteLine("班级编号重复,已存在"); } return classCount; } public static int ClassDelete(string[,] classDB, int classCount, string[,] studentDB, int studentCount) { //删除 Console.WriteLine("请输入你要删除的班级编号:"); string id = Znsd.GetString(); //有才可以删除 //校验 //思路:拿着id到数组里挨个去找是否有相同的 int index = -1; for (int i = 0; i < classCount; i++) { //for就能够将数组里的每一个都拿出来studentDB[i,0] if (classDB[i, 0].Equals(id)) { index = i;//把行号 赋值给index 0 1 2 3 4 } } if (index == -1)//没有重复的 { Console.WriteLine("班级编号meiyou找到 不能删"); } else { //找到了 删除 for (int i = index; i < classCount - 1; i++) { //一列一列的覆盖 classDB[i, 0] = classDB[i + 1, 0]; classDB[i, 1] = classDB[i + 1, 1]; } classCount--; //删除对应班级的学生 //??? for (int i = studentCount - 1; i >= 0; i--) { if (id.Equals(studentDB[i, 0])) { for (int j = i; j < studentCount - 1; j++) { studentDB[j, 0] = studentDB[j + 1, 0]; studentDB[j, 1] = studentDB[j + 1, 1]; studentDB[j, 2] = studentDB[j + 1, 2]; studentDB[j, 3] = studentDB[j + 1, 3]; } studentCount--; } } } return classCount; } public static void ClassUpdate(string[,] classDB, int classCount, string[,] studentDB, int studentCount) { Console.WriteLine("请输入你要修改的班级编号:"); string id = Znsd.GetString(); //校验 //思路:拿着id到数组里挨个去找是否有相同的 int index = -1; for (int i = 0; i < classCount; i++) { //for就能够将数组里的每一个都拿出来studentDB[i,0] if (classDB[i, 0].Equals(id)) { index = i;//把行号 赋值给index 0 1 2 3 4 } } if (index == -1)//没有重复的 { Console.WriteLine("班级编号 meiyou找到 不能xiugai"); } else { //如何修改 Console.WriteLine("请输入新的班级编号:"); string newId = Znsd.GetString(); int flag = -1; for (int i = 0; i < classCount; i++) { //for就能够将数组里的每一个都拿出来studentDB[i,0] if (classDB[i, 0].Equals(newId)) { flag = i;//把行号 赋值给index 0 1 2 3 4 } } if (flag == -1) { Console.WriteLine("请输入描述:"); string desc = Znsd.GetString(); //覆盖 classDB[index, 0] = newId; classDB[index, 1] = desc; //在学生数组里 对应的哪个班级编号也要修改 //??? for (int i = 0; i < studentCount; i++) { if (id.Equals(studentDB[i, 0])) { studentDB[i, 0] = newId; } } } else { Console.WriteLine("新的班级编号存在了。"); } } } public static void ClassSelect(string[,] classDB, int classCount) { for (int i = 0; i < classCount; i++) { Console.WriteLine("班级编号:{0};班级描述:{1}", classDB[i, 0], classDB[i, 1]); } } public static int StudentInsert(string[,] classDB, int classCount, string[,] studentDB, int studentCount) { Console.WriteLine("请输入这个学生要进入到哪个班级:"); string cid = Znsd.GetString(); //校验 //思路:拿着id到数组里挨个去找是否有相同的 int index = -1; for (int i = 0; i < classCount; i++) { //for就能够将数组里的每一个都拿出来stuDB[i,0] if (classDB[i, 0].Equals(cid)) { index = i;//把行号 赋值给index 0 1 2 3 4 } } if (index == -1)//没有重复的 { Console.WriteLine("班级编号bu存在"); } else { //增加逻辑 Console.WriteLine("请输入学号:"); string sid = Znsd.GetString(); //校验 //思路:拿着id到数组里挨个去找是否有相同的 int sindex = -1; for (int i = 0; i < studentCount; i++) { //for就能够将数组里的每一个都拿出来studentDB[i,0] if (studentDB[i, 0].Equals(sid)) { sindex = i;//把行号 赋值给index 0 1 2 3 4 } } if (sindex == -1)//没有重复的 { Console.WriteLine("请输入姓名:"); string sname = Znsd.GetString(); Console.WriteLine("请输入年龄:"); string sage = Znsd.GetString(); //将数据放入到容器 studentDB[studentCount, 0] = cid; studentDB[studentCount, 1] = sid; studentDB[studentCount, 2] = sname; studentDB[studentCount, 3] = sage; studentCount++; } else { Console.WriteLine("学号重复,已存在"); } } return studentCount; } public static int StudentDelete( string[,] studentDB, int studentCount) { //删除 Console.WriteLine("请输入一个学号:xxx"); string id = Znsd.GetString(); //有才可以删除 //校验 //思路:拿着id到数组里挨个去找是否有相同的 int index = -1; for (int i = 0; i < studentCount; i++) { //for就能够将数组里的每一个都拿出来studentDB[i,0] if (studentDB[i, 0].Equals(id)) { index = i;//把行号 赋值给index 0 1 2 3 4 } } if (index == -1)//没有重复的 { Console.WriteLine("学号meiyou找到 不能删"); } else { //找到了 删除 for (int i = index; i < studentCount - 1; i++) { //一列一列的覆盖 studentDB[i, 0] = studentDB[i + 1, 0]; studentDB[i, 1] = studentDB[i + 1, 1]; studentDB[i, 2] = studentDB[i + 1, 2]; } studentCount--; } return studentCount; } public static void StudentUpdate(string[,] studentDB, int studentCount) { Console.WriteLine("请输入你要修改的学号:"); string id = Znsd.GetString(); //校验 //思路:拿着id到数组里挨个去找是否有相同的 int index = -1; for (int i = 0; i < studentCount; i++) { //for就能够将数组里的每一个都拿出来studentDB[i,0] if (studentDB[i, 0].Equals(id)) { index = i;//把行号 赋值给index 0 1 2 3 4 } } if (index == -1)//没有重复的 { Console.WriteLine("学号meiyou找到 不能xiugai"); } else { //如何修改 Console.WriteLine("请输入新的学号:"); string newId = Znsd.GetString(); int flag = -1; for (int i = 0; i < studentCount; i++) { //for就能够将数组里的每一个都拿出来studentDB[i,0] if (studentDB[i, 0].Equals(newId)) { flag = i;//把行号 赋值给index 0 1 2 3 4 } } if (flag == -1) { Console.WriteLine("请输入姓名:"); string name = Znsd.GetString(); Console.WriteLine("请输入年龄:"); string age = Znsd.GetString(); //覆盖 studentDB[index, 0] = newId; studentDB[index, 1] = name; studentDB[index, 2] = age; } else { Console.WriteLine("新的学号存在了。"); } } } public static void StudentSelect(string[,] classDB, int classCount, string[,] studentDB, int studentCount) { for (int i = 0; i < classCount; i++) { Console.WriteLine("班级编号:{0};班级描述:{1}", classDB[i, 0], classDB[i, 1]); for (int j = 0; j < studentCount; j++) { if (classDB[i, 0].Equals(studentDB[j, 0])) { Console.WriteLine(" 学号:" + studentDB[j, 1] + ";姓名:" + studentDB[j, 2]); } } } } } } 结合这个系统,写出一个一样的程序,并且把对象、类加进去
06-28
设计一个学生选课信息管理系统,从屏幕读入学生、课程信息,执行学生选课操作,并显示选课结果。要求如下: (1)设计一个学生类Student,包括: 学号stuID、姓名stuName、学生对象的数量stuNum三个数据域; 一个无参构造方法,创建默认的学生,构造方法中输出“学生类无参构造方法”; 一个有参构造方法,创建指定学号stuID、姓名stuName的学生,构造方法中输出“学生类有参构造方法”; 所有数据域的访问器方法; 两个修改器方法,可以修改学号stuID、姓名stuName的值。 (2)设计一个课程Course,包括: 课程编号cID课程名cName课程对象的数量cNum三个数据域; 一个无参构造方法,创建默认的课程,构造方法中输出“课程类无参构造方法”; 一个有参构造方法,创建指定课程编号cID课程名cName课程,构造方法中输出“课程类有参构造方法”; 所有数据域的访问器方法; 两个修改器方法,可以修改课程编号cID课程名cName的值。 (3)设计一个学生选课类Schedule,包括: 学生列表stuList课程列表cList、学生选课总数schNum三个数据域,两个列表的默认长度任意设定; 一个无参构造方法,创建默认的学生选课对象; 一个学生选课方法 addCourse(Student stu,Course course),实现学生stu选择课程course操作; 一个显示学生选课详情方法 displayCourse(),显示所有学生选课情况。 (4)测试类Main,要求: 情况1 test1: ① 使用无参构造方法建立二个学生对象; ② 查看学生对象总数 情况2 test2: ① 使用无参构造方法建立三门课程对象; ② 查看课程对象总数 情况3 test3: ① 使用有参构造方法建立一个学生对象; ② 使用无参构造方法建立二门课程对象; ③ 使用学生选课类进行课程选择,为学生选择这两门课程 ④ 查看学生选课总数 ⑤ 查看学生选课详情 情况4 test4: ① 使用有参构造方法建立三个学生对象; ② 使用有参构造方法建立四门课程; ③ 使用学生选课类进行课程选择 第一个学生选择课程2、课程3; 第二个学生选择课程1; 第三个学生选择课程1、课程2、课程4。 ④ 查看选课信息 查看学生对
04-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值