字符串排列问题拓展

来源:互联网 发布:洞主的生活湖淘宝网店 编辑:程序博客网 时间:2024/05/02 11:18

题目:
这里写图片描述

解决思路:先将8个数字的数组的排列都找出来,然后判断三对相对面上的顶点之和是否相等,如果相等的话就保留下来,如果不相等就不要。

字符串排列问题在这里:字符串排列问题
代码如下:

/** *  */package problem2;import java.util.ArrayList;  import java.util.List;  /** * @author Hutongling * * @time:2017年3月22日 下午8:54:50 */public class 字符串排列问题拓展 {      /**      * @param base 以该字符串作为基础字符串,进行选择性组合。      * @param buff 所求字符串的临时结果      * @param result 存放所求结果      */      public static List<String> list(String base,String buff){          List<String> result = new ArrayList<String>();//存放结果信息。          if(base.length()<=0)                    result.add(buff);        for(int i=0;i<base.length();i++){              List<String> temp = list(new StringBuffer(base).deleteCharAt(i).toString(),buff+base.charAt(i));              result.addAll(temp);          }          return result;      }      public static void main(String[] args) {        String s = "12345678";// 原字符串        if (s == null || s.length() == 0)            System.out.println("请输入字符串");        else {            List<String> result = list(s, "");// 列出字符的组合,放入result            System.out.print("一共有"+result.size()+"种情况:");            System.out.println(result);            //计算当把八个数字放在一个立方体的八个角上的时候,三组相对的面的四个角的和是否相等            List<String> result1=new ArrayList<String>();            int count=0;            for(int i=0;i<result.size();i++){                int a1=result.get(i).charAt(0)-'0';                int a2=result.get(i).charAt(1)-'0';                int a3=result.get(i).charAt(2)-'0';                int a4=result.get(i).charAt(3)-'0';                int a5=result.get(i).charAt(4)-'0';                int a6=result.get(i).charAt(5)-'0';                int a7=result.get(i).charAt(6)-'0';                int a8=result.get(i).charAt(7)-'0';                if((a1+a2+a3+a4==a5+a6+a7+a8)&&(a1+a3+a5+a7==a2+a4+a6+a8)&&(a1+a2+a5+a6==a3+a4+a7+a8))                    {                        count++;                        result1.add(result.get(i));                    }            }            System.out.println("符合条件的有" + count+ "个:");            System.out.println(result1);        }    }  }  

代码结果:
一共有40320种排列情况
符合条件的有144个:
[03567421, 03657412, 03745621, 03746512, 05367241, 05637214, 05723641, 05726314, 06357142, 06537124, 06713542, 06715324, 07345261, 07346152, 07523461, 07526134, 07613452, 07615234, 12476530, 12654730, 12657403, 12746503, 14276350, 14632750, 14637205, 14726305, 16254370, 16257043, 16432570, 16437025, 16702543, 16704325, 17246053, 17426035, 17602453, 17604235, 21475630, 21564730, 21567403, 21745603, 24175360, 24531760, 24537106, 24715306, 25164370, 25167043, 25431670, 25437016, 25701643, 25704316, 27145063, 27415036, 27501463, 27504136, 30475621, 30476512, 30564721, 30654712, 34075261, 34076152, 34520761, 34526107, 34610752, 34615207, 35064271, 35420671, 35426017, 35604217, 36054172, 36410572, 36415027, 36504127, 41273650, 41362750, 41367205, 41723605, 42173560, 42351760, 42357106, 42713506, 43162570, 43167025, 43251670, 43257016, 43701625, 43702516, 47123065, 47213056, 47301265, 47302156, 50273641, 50276314, 50362741, 50632714, 52073461, 52076134, 52340761, 52346107, 52610734, 52613407, 53062471, 53240671, 53246017, 53602417, 56032174, 56210374, 56213047, 56302147, 60173542, 60175324, 60351742, 60531724, 61073452, 61075234, 61340752, 61345207, 61520734, 61523407, 63051472, 63140572, 63145027, 63501427, 65031274, 65120374, 65123047, 65301247, 70162543, 70164325, 70251643, 70254316, 70431625, 70432516, 71062453, 71064235, 71240653, 71420635, 72051463, 72054136, 72140563, 72410536, 74031265, 74032156, 74120365, 74210356]

0 0
原创粉丝点击