首页 > 其他分享 >张昌蒲 严教 卡牌计算器

张昌蒲 严教 卡牌计算器

时间:2023-02-26 10:34:16浏览次数:31  
标签:int List list 卡牌 张昌 getValue 严教 new com

1.描述

  玩过三国杀的朋友应该都知道有个将叫张昌蒲,他会随机给出N张卡牌,卡牌的点数在1-13之间,可能重复。现在要求把卡牌分为两堆,两边卡牌的点数之和必须相等,要求两堆的卡牌数之和尽量多。

  针对这个逻辑,我自己写了一些JAVA的实现,经过测试可行,下面给出代码,有兴趣的朋友可以看看,有改进的地方欢迎指出!

2.代码如下

计算分配方式的代码:

  1 public class CalculateCard {
  2 
  3     public static void main(String[] args) {
  4         Scanner scanner = new Scanner(System.in);
  5         System.out.println("请输入严教牌,按点数,以空格割开,如(13 11 10):");
  6         String input = scanner.nextLine();
  7         System.out.println(input);
  8         String[] inputCards = input.split(" ");
  9         List<Integer> list = new ArrayList<>();
 10         try {
 11             Arrays.stream(inputCards).forEach(card -> list.add(Integer.parseInt(card)));
 12         }catch (NumberFormatException e) {
 13             System.out.println("请输入数字,牌的点数在1-13");
 14         }
 15         AtomicBoolean isCorrect = new AtomicBoolean(true);
 16         list.forEach(item -> isCorrect.set(item <= 13 && item >= 1));
 17         if(!isCorrect.get()){
 18             System.out.println("----请输入数字,牌的点数在1-13");
 19         }
 20         calculateAll(list);
 21     }
 22 
 23     /**
 24      * 计算所有可能的分配方式,并找到使用卡牌最多的组合方式
 25      * @param list 所有输入的数字
 26      */
 27     private static void calculateAll(List<Integer> list){
 28         List<Pair<Integer,Pair<String,String>>> okResult = new ArrayList<>();
 29         List<Pair<Integer,Pair<String,String>>> okResultLast = new ArrayList<>();
 30         List<Pair<Integer,Pair<Integer,String>>> result = new ArrayList<>();
 31         //获取所有的数字组合方式
 32         getAllComb(list,result);
 33         int countNum = 0;
 34         //遍历所有组合方式
 35         for (int i = 0; i < result.size(); i++) {
 36             /*
 37              * 思路
 38              * 1.取出其中一种组合,拿到这个组合的点数和sum
 39              * 2.将当前组合中的所有数字放在一个List--currentList中
 40              * 3.从原来得包含所有点数的卡牌集合中筛选出去掉currentList所有数字的其他数字,放在listLeft中
 41              * 5.获得listLeft中所有数字的全部组合方式,放在result1中
 42              * 6.遍历result1,比对总和与sum是否相等,相等则为一种成功的组合方式。
 43              * 7.找到成功组合中使用卡牌最多的组合方式
 44              */
 45             Pair<Integer,Pair<Integer,String>> pair = result.get(i);
 46             int sum = pair.getValue().getKey();
 47             int numCount = pair.getKey();
 48             String com = pair.getValue().getValue();
 49             String[] numStr = com.split(",");
 50             List<Integer> listLeft = new ArrayList<>();
 51             List<Integer> currentList = new ArrayList<>();
 52             for (int j = 0; j < numStr.length; j++) {
 53                 currentList.add(Integer.parseInt(numStr[j]));
 54             }
 55             for (int j = 0; j < list.size(); j++) {
 56                 if(!currentList.contains(list.get(j))) listLeft.add(list.get(j));
 57             }
 58             List<Pair<Integer,Pair<Integer,String>>> result1 = new ArrayList<>();
 59             getAllComb(listLeft,result1);
 60             for (int j = 0; j < result1.size(); j++) {
 61                 Pair<Integer,Pair<Integer,String>> pair1 = result1.get(j);
 62                 int leftSum = pair1.getValue().getKey();
 63                 int leftCount = pair1.getKey();
 64                 String com1 = pair1.getValue().getValue();
 65                 countNum = countNum == 0 ? numCount + leftCount : countNum;
 66                 if(leftSum == sum && countNum <= numCount + leftCount){
 67                     //计数,如果组合中的数字个数更大,则移除结果集,重新添加,只保存最多的
 68                     if(countNum != 0 && countNum < numCount+leftCount){
 69                         countNum = numCount + leftCount;
 70                         okResultLast.clear();
 71                     }
 72                     okResult.add(new Pair<>(numCount+leftCount,new Pair<>(com,com1)));
 73                     okResultLast.add(new Pair<>(numCount+leftCount,new Pair<>(com,com1)));
 74                 }
 75             }
 76         }
 77 //        System.out.println("所有组合:");
 78 //        for (int i = 0; i < okResult.size(); i++) {
 79 //            Pair<Integer,Pair<String,String>> resultPair = okResult.get(i);
 80 //            System.out.println("组合的总牌数:"+resultPair.getKey() +
 81 //                    "  ===>组合1为:  " + resultPair.getValue().getKey() +
 82 //                    "  ===>组合2为:  " + resultPair.getValue().getValue());
 83 //        }
 84         System.out.println("=================================================");
 85         System.out.println("使用卡牌最多的组合方式:");
 86         System.out.println("=================================================");
 87         for (int i = 0; i < okResultLast.size(); i++) {
 88             Pair<Integer,Pair<String,String>> resultPair = okResultLast.get(i);
 89             System.out.println("当前分配方"+i+"式总牌数:"+resultPair.getKey() +
 90                                "\n====>堆一卡牌点数分别为:  " + resultPair.getValue().getKey() +
 91                                "\n====>堆二卡牌点数分别为:  " + resultPair.getValue().getValue());
 92         }
 93         System.out.println("=================================================");
 94     }
 95 
 96     /**
 97      * 获得所有可能的组合
 98      * 如数字列表为:【1,2,3】
 99      * 那么得到的所有组合为【1】,【2】,【3】,【1,2】,【1,3】,【2,3】,【1,2,3】
100      * @param list 所有数字
101      * @param result 返回的组合集合
102      */
103     private static void getAllComb(List<Integer> list,List<Pair<Integer,Pair<Integer,String>>> result){
104         List<String> com = new ArrayList<>();
105         for (int i = 1; i != list.size() + 1; i++) {
106             ComputCombinations.getCombination(result,list,0,i,com);
107         }
108 //        result.stream().forEach(item -> System.out.println("数字数:"+ item.getKey()+
109 //                "=====>总值:"+ item.getValue().getKey()+ " 内容:" + item.getValue().getValue()));
110     }
111 
112 }

计算数字组合方式的代码:

 public static void getCombination(List<Pair<Integer,Pair<Integer,String>>> result,
                                                    List<Integer> list, int start, int len , List<String> com){
        if(len == 0){
            String s = "";
            for (int i = 0; i != com.size() ; i++) {
                s = s.concat(com.get(i));
            }
            String comStr = s.substring(0,s.length() - 1);
            String[] all = comStr.split(",");
            AtomicInteger sum = new AtomicInteger();
            Arrays.stream(all).forEach(item -> sum.addAndGet(Integer.parseInt(item)));
            result.add(new Pair<>(com.size(),new Pair<>(sum.get(),comStr)));
            return;
        }
        if(start == list.size()){
            return;
        }
        com.add(list.get(start)+",");
        getCombination(result,list, start+1,len - 1,com);
        com.remove(com.size()-1);
        getCombination(result,list,start+1,len,com);
    }

测试结果:

 

 

 

有可以改进的地方,欢迎指出...

标签:int,List,list,卡牌,张昌,getValue,严教,new,com
From: https://www.cnblogs.com/coderChengL/p/zhangchangpuCalculate.html

相关文章

  • P8292 [省选联考 2022] 卡牌
    我决定不整什么写过的题的集合了,写不过来。想到啥题好就写啥。这题是个很好的套路。考虑到值域不怎么大,想到根号分治。也就是小于根号的质数不超过\(14\)个,大于根号的......
  • 「解题报告」[省选联考 2022] 卡牌
    放假上午想出的做法,写了一下TLE35分。以为有更高级的复杂度,然后刚看了看题解发现题解就是这个复杂度,呃呃,卡常吧。考虑将每个数写成它所包含的质因子的集合,写成一个0......
  • 小 A 的卡牌游戏
    小A的卡牌游戏小A最近沉迷于一款名为Hearthverse的卡牌游戏。在这款游戏中,卡被分为了三个种类(随从、法术和魔法阵),在组卡时,这款游戏严格规定了卡组中每种卡牌的数量,......
  • 如何制作一个羊了个羊游戏3:卡牌拾取和消除
    本文首发于微信公众号【小蚂蚁教你做游戏】,欢迎关注领取更多学习做游戏的原创教程资料,每天学点儿游戏开发知识。嗨!大家好,我是小蚂蚁。终于要写第三篇教程了,中间拖的时间有点......
  • 如何制作一个羊了个羊游戏2:随机生成卡牌
    本文首发于微信公众号【小蚂蚁教你做游戏】,欢迎关注领取更多学习做游戏的原创教程资料,每天学点儿游戏开发知识。嗨!大家好,我是小蚂蚁。上一节教程里我们实现了游戏中最难的地......
  • 堆叠大陆 Stacklands for Mac(卡牌游戏) v1.1.6中文原生版
    Stacklands中文名《堆叠大陆》又名《堆叠世界》《层叠世界》,是一款可以在mac上运行的村庄建设卡牌游戏,你可以在那里堆叠卡牌以收集食物、修建建筑、并与各种生物战斗。游戏......
  • 2021.09 ccf csp 第四题 收集卡牌
    2021.09ccfcsp第四题收集卡牌思路这题如果直接计算,因为不同的分类种数太多,枚举所有的分类情况是一个几乎不可能的复杂任务。但不同摸牌次数,不同已摸出牌种类的子问......
  • 《五行学徒》卡牌构筑游戏Demo【四人独立项目】
    Apk下载(蓝奏云)密码:3ggc制作时间:2020年9月--2020年10月在Unity大学的实操项目,一个基于U3D开发的卡牌构筑游戏,参考《万智牌》的"五属"定下了五行的主题,参考《杀......
  • P8292 [省选联考 2022] 卡牌
    https://www.luogu.com.cn/problem/P8292题解先把小于等于\(\sqrt{2000}\)的质数打一个表,发现只有\(14\)个,其中第\(14\)个是\(43\).令前\(14\)个质数为小质数,其它的......
  • 【过程】游戏王简单卡牌检索
    通过这样的代码显示我想要的内容aa=newa();while(hao==1){a.a();break;}这个是输入了名字之后会显示的publicvoida(){System.out.println("卡名:青眼白......