Browse Source

计算排列数

duh 6 years ago
parent
commit
fcadbd4725

+ 1 - 1
src/main/java/controller/MyPanel.java

@@ -50,7 +50,7 @@ public class MyPanel extends JPanel{
 //        drawTemplateAndMove(room,g);
 
         try {
-            ImageIO.write(bufferedImage, "jpeg", new File("jpanel.jpg"));
+            ImageIO.write(bufferedImage, "jpeg", new File("E:\\config\\jpanel.jpg"));
         } catch (IOException e) {
             e.printStackTrace();
         }

+ 72 - 0
src/main/java/utils/CalcYizhiArrayUtils.java

@@ -0,0 +1,72 @@
+package utils;
+
+import model.Room;
+import model.template.Template;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @author duh
+ * @create 2018/7/25 14:53
+ * @email duh@elab-plus.com
+ **/
+public class CalcYizhiArrayUtils {
+    private List<Integer> roomLength;
+    private List<Integer> oneAlignAllLength;
+    private List<Integer> twoAlignAllShortLength;
+    private List<Integer> twoAlignAllLongLength;
+    public void calcYizhiWithOnlyOneTwoAlign(){
+        int totalOneLenth = 0;
+        for(int length : oneAlignAllLength){
+            totalOneLenth += length;
+        }
+        int leftForTwo = roomLength.get(3) - totalOneLenth;
+        if(leftForTwo>0){
+            System.out.println("可以一字排,方案如下:");
+        }
+    }
+
+    /**
+     * 获取n个数的所有组合
+     * @param n
+     */
+    public List<List<Integer>> getAllArrayList(int n){
+        List<List<Integer>> arrayList = new ArrayList<>();
+        if(n < 1){
+            return arrayList;
+        }else if(n==1){
+            List<Integer> list = new ArrayList<>();
+            list.add(n);
+            arrayList.add(list);
+            return arrayList;
+        }
+        arrayList = getAllArrayList(n-1);
+        for(int index = arrayList.size() -1;index>=0;index--){
+            List<Integer> list = arrayList.remove(index);
+            for(int i = 0;i<n;i++){
+                List<Integer> newList = new ArrayList<>();
+                newList.addAll(list);
+                newList.add(i,n);
+                arrayList.add(newList);
+            }
+        }
+        return arrayList;
+    }
+
+    /**
+     * 排列数
+     * @param n
+     * @return
+     */
+    public int A(int n){
+        if(n <= 0){
+            return 0;
+        }else if(n==1){
+            return 1;
+        }else {
+            return n*A(n-1);
+        }
+    }
+}

+ 22 - 0
src/test/java/utils/CalcYizhiArrayUtilsTest.java

@@ -0,0 +1,22 @@
+package utils;
+
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.junit.Assert.*;
+
+public class CalcYizhiArrayUtilsTest {
+    private CalcYizhiArrayUtils calcYizhiArrayUtils = new CalcYizhiArrayUtils();
+    @Test
+    public void getAllArrayList() {
+        System.out.println(Integer.MAX_VALUE);
+        System.out.println(Integer.MIN_VALUE);
+        for(int i =1;i<16;i++){
+            System.out.println("A("+i+")="+calcYizhiArrayUtils.A(i));
+        }
+//        int n = 6;
+//        List<List<Integer>> list = calcYizhiArrayUtils.getAllArrayList(n);
+//        list.forEach(x-> System.out.println(x));
+    }
+}