学生选课

 1 JAVA编程实现如下需求:
 2  
 3 2017年05期培训班组织技术学习与分享,有如下技术可供选择:
 4 VirtualBox
 5 Vagrant
 6 WebSocket
 7 JSONP
 8 Redis
 9 MongoDB
10 Cassandra
11 RabbitMQ
12 ActiveMQ
13 Kafka
14 Lucene
15 Solr
16 ElasticSearch
17 Hadoop
18 HDFS
19 HIVE
20 PIG
21 Mahout
22 HBase
23 Spark
24 Guava
25 Protobuf
26 Avro
27 Thrift
28 Motan
29 Docker
30 DynamoDB
31 Scala
32 Groovy
33 SpringBoot
34  
35 学员每人选择其中两项进行学习,并在学习会以Demo的形式分享给其他同事。学员们的意向如下:
36 吕鹏飞 ElasticSearch Redis
37 丁虎 Redis SpringBoot
38 梁秀斗 Hadoop HDFS
39 李文鹏 Docker Kafka 
40 苗恒飞 Lucene Solr
41 佘昊 Solr Redis
42 杜世阳 ActiveMQ Hadoop
43 刘翩 SpringBoot ActiveMQ
44 史建智 Docker Lucene
45 王帅 Cassandra Spark
46 张昌昌 SpringBoot MongoDB
47 王腾飞 SpringBoot Spark
48 杨小平 WebSocket RabbitMQ
49  
50 请编写程序为学员安排最终的技术学习清单,要求:
51  
52 · 如果一项技术只有一个学员选择,则直接为该学员指定该技术
53  
54 · 如果一项技术有多个学员选择,则在选择了该项技术的学员中随机指定一位学习该技术
55  
56 · 如果一个学员被指定的技术不足两项,则在未被指定的技术中随机指定一项或两项给该学员,以凑足两项但不能多于两项。
57  
58 · 每个学员被指定的技术不能重复
59  
60 · 需要输出最终的技术指定清单
61  
62 · 需要输出未被指定给学员的技术清单。
  1 import java.util.ArrayList;
  2 import java.util.HashMap;
  3 import java.util.List;
  4 import java.util.Map;
  5 import java.util.Random;
  6 import java.util.Set;
  7 
  8 public class Course {
  9     public static void main(String[] args) {
 10         
 11         Map<String,String> map = initChooseCourse();//学生选课的初始数据
 12         List<String> list = initAllCourse();//所有课程
 13         Map<String,String> rmap = new HashMap<>();//最后选课结果
 14         
 15         Map<String,String> nmap = reverseMap(map); //key-value颠倒,重复的key拼接到新value里
 16         System.out.println("=====颠倒=====");
 17         print(nmap);
 18         list.removeAll(nmap.keySet());  //剩余课程
 19         rmap = distinctMap(nmap);
 20         System.out.println("=====去重=====");
 21         print(rmap);
 22         
 23         buqiCourse(rmap,list);    //每个学生补齐至2门课
 24 
 25         System.out.println("===========学员的技术学习清单============");
 26         
 27         System.out.println("=====开始=====");
 28         print(map);
 29         
 30         System.out.println("=====最终=====");
 31         print(rmap);
 32         
 33         System.out.println("========未被指定给学员的技术清单==========");
 34         System.out.println(list);
 35     }
 36     //每个学生补齐至2门课
 37     public static void buqiCourse(Map<String,String> rmap,List<String> list){
 38         Set<String> set = rmap.keySet();
 39         Random r = new Random();
 40         int ram;
 41         for (String s : set) {
 42             if(rmap.get(s).equals("")) {
 43                 ram = r.nextInt(list.size());
 44                 String course = list.get(ram) + " ";
 45                 list.remove(ram);
 46                 ram = r.nextInt(list.size());
 47                 course += list.get(ram);
 48                 list.remove(ram);
 49                 rmap.put(s, course);
 50             }else if(!rmap.get(s).equals("") && !rmap.get(s).contains(" ")) {
 51                 ram = r.nextInt(list.size());
 52                 rmap.put(s, rmap.get(s) + " " + list.get(ram));
 53                 list.remove(ram);
 54             }
 55         }
 56         //return list;
 57     }
 58     /*
 59      * 课程- 学生 集合 转换成 学生-课程集合,
 60      * 如果一项技术只有一个学员选择,则直接为该学员指定该技术,
 61      * 并随机指定一位学生选择多个学生选择的课程,
 62      * 其余学生去掉此课程,没有课程的学生制空
 63      * */
 64     public static Map<String,String> distinctMap(Map<String,String> map){
 65         Map<String,String> rmap = new HashMap<>();
 66         Set<String> nset = map.keySet();
 67         Random r = new Random();
 68         for (String ns : nset) {
 69             String[] str = map.get(ns).split(",");
 70             if(str.length == 1) {
 71                 if(rmap.containsKey(str[0]) && !rmap.get(str[0]).equals("")) {
 72                     rmap.put(str[0], rmap.get(str[0]) + " " + ns);
 73                 } else {
 74                     rmap.put(str[0], ns);
 75                 }
 76             }else {
 77                 int temp = r.nextInt(str.length);
 78                 for(int i = 0; i < str.length; i++) {
 79                     if(!rmap.containsKey(str[i]) && i != temp) {
 80                         rmap.put(str[i], "");
 81                     }else if(!rmap.containsKey(str[i]) && i == temp) {
 82                         rmap.put(str[i], ns);
 83                     }else if(rmap.containsKey(str[i]) && i == temp) {
 84                         if(rmap.get(str[i]).equals("")) {
 85                             rmap.put(str[i], ns);
 86                         } else {
 87                             rmap.put(str[i], rmap.get(str[i]) + " " + ns);
 88                         }
 89                     }
 90                 }
 91             }
 92         }
 93         return rmap;
 94     }
 95     //学生-课程 集合 转换成 课程- 学生集合
 96     public static Map<String,String> reverseMap(Map<String,String> map){
 97         Map<String,String> nmap = new HashMap<>();
 98         Set<String> set = map.keySet();
 99         for (String s : set) {
100             String[] str = map.get(s).split(" ");
101             for (String ss : str) {
102                 if(nmap.containsKey(ss)) {
103                     nmap.put(ss, nmap.get(ss) + "," + s);
104                 }else {
105                     nmap.put(ss, s);
106                 }
107             }
108         }
109         return nmap;
110     }
111     //打印map集合
112     public static void print(Map<String,String> map) {
113         Set<String> set = map.keySet();
114         for (String s : set) {
115             System.out.println(s + ": " + map.get(s));
116         }
117         System.out.println();
118     }
119     
120     //初始选课
121     public static Map<String,String> initChooseCourse(){  //15个
122         Map<String,String> map = new HashMap<String, String>();
123         map.put("吕鹏飞","ElasticSearch Redis");
124         map.put("丁虎","Redis SpringBoot");
125         map.put("梁秀斗","Hadoop HDFS");
126         map.put("李文鹏","Docker Kafka ");
127         map.put("苗桓飞","Lucene Solr");
128         map.put("佘昊","Solr Redis");
129         map.put("杜世阳","ActiveMQ Hadoop");
130         map.put("刘翩","SpringBoot ActiveMQ");
131         map.put("史建智","Docker Lucene");
132         map.put("王帅","Cassandra Spark");
133         map.put("张昌昌","SpringBoot MongoDB");
134         map.put("王腾飞","SpringBoot Spark");
135         map.put("杨小平","WebSocket RabbitMQ");
136         return map;
137     }
138     
139     //所有课程
140     public static List<String> initAllCourse(){
141         List<String> list = new ArrayList<>();
142         list.add("ActiveMQ");
143         list.add("Avro");
144         list.add("Cassandra");
145         list.add("Docker");
146         list.add("DynamoDB");
147         list.add("ElasticSearch");
148         list.add("Groovy");
149         list.add("Guava");
150         list.add("Hadoop");
151         list.add("HBase");
152         list.add("HIVE");
153         list.add("HDFS");
154         list.add("JSONP");
155         list.add("Kafka");
156         list.add("Lucene");
157         list.add("Mahout");
158         list.add("MongoDB");
159         list.add("Motan");
160         list.add("PIG");
161         list.add("Protobuf");
162         list.add("RabbitMQ");
163         list.add("Redis");
164         list.add("Scala");
165         list.add("Solr");
166         list.add("Spark");
167         list.add("SpringBoot");
168         list.add("Thrift");
169         list.add("Vagrant");
170         list.add("VirtualBox");
171         list.add("WebSocket");
172         return list;
173     }
174 }
原文地址:https://www.cnblogs.com/archimedes-euler/p/9975813.html