/*** 生成搭档列表* @param peopleList 参赛人列表* @param roundNum 每人场次 默认 人数-1* @return 所有搭档列表*/public static<T> List<List<T>> getPartnerList(List<T> peopleList, int roundNum) {//总人数int totalNum = peopleList.size();// 所有有可能搭档的组List<List<T>> partnerGroupList = new ArrayList<>();// 出场的搭档数组List<List<T>> partnerGroupListReal = new ArrayList<>();// 记录每人上场的次数对象Map<T, Integer> pShouldRoundNum = new HashMap<>();int groundNum = 0;for (int i = 0; i < peopleList.size(); i++) {// 预先定下前面的搭档池数pShouldRoundNum.(i), peopleList.size() - 1);}// 整除if ((totalNum * roundNum) % 4 == 0) {groundNum = (int) Math.floor((double) (peopleList.size() * roundNum) / 2);for (int i = 0; i < peopleList.size(); i++) {T e = (i);for (int j = i + 1; j < peopleList.size(); j++) {T e1 = (j);List<T> partnerGroup = new ArrayList<>();partnerGroup.add(e);partnerGroup.add(e1);// 将搭档添加到搭档组列表中partnerGroupList.add(partnerGroup);}}// 将所有搭档组添加到实际搭档列表中partnerGroupListReal.addAll(partnerGroupList);// 必须每人多打几场if (roundNum > peopleList.size() - 1) {// 创建临时搭档组列表,用于随机选择搭档组进行调整List<List<T>> tempPartnerGroupList = new ArrayList<>(partnerGroupList);boolean flag = true;Random rand = new Random();while (flag) {// 从临时搭档组列表中随机选择一个搭档组的索引int index = Int(tempPartnerGroupList.size());// 从临时搭档组列表中移除选中的搭档组List<T> partnerGroupListRealOne = ve(index);// 获取第一个选手T p1 = (0);// 获取第二个选手T p2 = (1);// 如果两个选手还未达到指定场次,则将该搭档组加入实际搭档列表中,并更新他们的场次计数器if ((p1) < roundNum && (p2) < roundNum) {partnerGroupListReal.add(partnerGroupListRealOne);pShouldRoundNum.put(p1, (p1) + 1);pShouldRoundNum.put(p2, (p2) + 1);}if (partnerGroupListReal.size() == groundNum) {flag = false;}}}return partnerGroupListReal;} else {return new ArrayList<>();}}
/*** 生成对战表* @param peopleList 参赛人列表* @param roundNum 每人场次 默认 人数-1* @return 对战信息*/public static <T> List<List<List<T>>> getBattleList(List<T> peopleList, int roundNum) {while ((peopleList.size() * roundNum) % 4 != 0) {roundNum++;}//生成搭档池子List<List<T>> matchListPoor = getPartnerList(peopleList, roundNum);//总场次int allRound = matchListPoor.size() / 2;List<List<List<T>>> res = new ArrayList<>();List<List<T>> rivalry = new ArrayList<>();int round = 0;while (matchListPoor.size() > 0) {List<T> per = (0);ve(0);if (rivalry.size() == 0) {rivalry.add(per);round = 1;} else if (!(0).(0)) && !(0).(1))) {rivalry.add(per);} else {matchListPoor.add(per);round = 2;}if (rivalry.size() == 2) {res.add(Arrays.(0), (1)));rivalry.clear();}if (matchListPoor.size() == 1 && round == 2) {matchListPoor.addAll(rivalry);for (int j = 0; j < res.size(); j++) {if (!(j).get(0).(0).get(0)) && !(j).get(0).(0).get(0))) {List<T> temp = (j).get(1);(j).set(1, (0));matchListPoor.set(0, temp);if (!(0).(1).get(0)) && !(0).(1).get(1))) {res.add(Arrays.(0), (1)));break;}}}}if (res.size() == allRound) {break;}}return sort(res);}
生成的对战表是随机的,所以有可能会出现有人连打好几场
/*** 排序(避免有人连打多场)* @param oldMatchList 初始对战表* @return 排序后对战表*/private static <T> List<List<List<T>>> sort(List<List<List<T>>> oldMatchList) {//排序后新对战表List<List<List<T>>> newMatchList = new ArrayList<>();newMatchList.(0));ve(0);while (oldMatchList.size() >0){//新对战表最后一组List<List<T>> lastList = (newMatchList.size() - 1);//最后一组的人List<T> lastMember = new ArrayList<>();for (List<T> list : lastList) {lastMember.addAll(list);}for (int i = oldMatchList.size()-1; i >=0; i--) {//旧对战表中每一局的人List<T> member = new ArrayList<>();for (List<T> list : (i)) {member.addAll(list);}if (!(0)) && !(1)) && !(2)) && !(3))){newMatchList.(i));ve(i);break;}if (i == 0){newMatchList.(i));ve(i);break;}}}return newMatchList;}
public static void main(String[] args) {List<String> peopleList = new ArrayList<>(Arrays.asList("李逍遥","张无忌","张三丰","杨不悔","周芷若","俞岱岩","宋远桥","常玉春"));List<List<List<String>>> battleList = getBattleList(peopleList, 7);List<String> battle = new ArrayList<>();for (int i = 0; i < battleList.size(); i++) {battle.add(String.format("第%s场: %s VS %s",i+1,String.join("/",(i).get(0)),String.join("/",(i).get(1))));}battle.forEach(System.out::println);}
运行结果
本文发布于:2024-01-31 20:01:34,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170670249431012.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |