2016-11-05 86 views
-1

我試圖用+ - */ 打印所有獨特的可能性1 + 2-3 * 4/5 另一種可能性是將數字重新排列或操作重新排列。我認爲我的錯誤是一個無限循環,但它最後打印了最終的術語47。請幫忙。打印唯一編號

import java.util.Scanner; 
import java.lang.Math; 

class Main { 
    public static void main(String[] args) { 

     Scanner scan = new Scanner(System.in); 

     int[] all = new int[47]; 

     System.out.print(all.length); 

     int b = 0; 

     int c = 0; 
     for (int count = 0; count < 47;) { 

      int num1 = (int) (Math.random() * 5 + 1); 

      int num2 = num1; 

      while (num2 == num1) { 
       num2 = (int) (Math.random() * 5 + 1); 
      } 

      int num3 = num2; 

      while ((num3 == num1) || (num3 == num2)) { 
       num3 = (int) (Math.random() * 5 + 1); 

      } 

      int num4 = num3; 

      while ((num4 == num1) || (num4 == num2) || (num4 == num3)) { 
       num4 = (int) (Math.random() * 5 + 1); 
      } 

      int num5 = num4; 

      while ((num5 == num1) || (num5 == num2) || (num5 == num3) || (num5 == num4)) { 
       num5 = (int) (Math.random() * 5 + 1); 
      } 

      int numbers1 = num1 + num2 - num3 * num4/num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers1) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers1; 
        count++; 
       } 

      } 
      int numbers2 = num1 + num2 - num3/num4 * num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers2) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers2; 
        count++; 
       } 
      } 
      int numbers3 = num1 + num2/num3 - num4 * num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers3) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers3; 
        count++; 
       } 
      } 
      int numbers4 = num1 + num2/num3 * num4 - num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers4) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers4; 
        count++; 
       } 
      } 

      int numbers5 = num1 + num2 * num3/num4 - num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers5) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers5; 
        count++; 
       } 
      } 
      int numbers6 = num1 + num2 * num3 - num4/num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers6) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers6; 
        count++; 
       } 
      } 
      int numbers7 = num1 - num2 + num3/num4 * num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers7) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers7; 
        count++; 
       } 
      } 

      int numbers8 = num1 - num2 + num3 * num4/num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers8) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers8; 
        count++; 
       } 
      } 

      int numbers9 = num1 - num2/num3 + num4 * num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers9) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers9; 
        count++; 
       } 
      } 
      int numbers10 = num1 - num2/num3 * num4 + num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers10) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers10; 
        count++; 
       } 
      } 
      int numbers11 = num1 - num2 * num3/num4 + num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers11) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers11; 
        count++; 
       } 
      } 
      int numbers12 = num1 - num2 * num3 + num4/num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers12) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers12; 
        count++; 
       } 
      } 

      int numbers13 = num1/num2 * num3 - num4 + num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers13) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers13; 
        count++; 
       } 
      } 
      int numbers14 = num1/num2 * num3 + num4 - num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers14) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers14; 
        count++; 
       } 
      } 
      int numbers15 = num1/num2 + num3 * num4 - num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers15) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers15; 
        count++; 
       } 
      } 
      int numbers16 = num1/num2 + num3 - num4 * num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers16) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers16; 
        count++; 
       } 
      } 
      int numbers17 = num1/num2 - num3 * num4 + num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers17) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers17; 
        count++; 
       } 
      } 
      int numbers18 = num1/num2 - num3 + num4 * num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers18) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers18; 
        count++; 
       } 
      } 
      int numbers19 = num1 * num2/num3 - num4 + num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers19) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers19; 
        count++; 
       } 
      } 
      int numbers20 = num1 * num2/num3 + num4 - num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers20) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers20; 
        count++; 
       } 
      } 
      int numbers21 = num1 * num2 + num3 - num4/num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers21) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers21; 
        count++; 
       } 
      } 

      int numbers22 = num1 * num2 + num3/num4 - num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers22) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers22; 
        count++; 
       } 
      } 

      int numbers23 = num1 * num2 - num3/num4 + num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers23) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers23; 
        count++; 
       } 
      } 
      int numbers24 = num1 * num2 - num3 + num4/num5; 

      for (int i = 0; i < all.length; i++) 

      { 
       if (all[i] == numbers24) { 
        b = 1; 
       } 

       if ((b != 1) && (i == (all.length - 1))) { 
        all[count] = numbers24; 
        count++; 
       } 
      } 

     } 

     for (c = 0; c < all.length; c++) { 
      if (c > 0) { 
       System.out.print(", "); 
      } 
      System.out.print(all[c]); 

     } 
     System.out.print(all.length); 

    } 

} 
+0

您需要張貼較少的代碼。這對於期望任何人挖掘的太多了。你試過調試它嗎? – Carcigenicate

回答

0

在循環開始之前,您的「最終」項47被打印爲main方法中的第三行。因爲你正在打印所有數組的長度。

System.out.print(all.length);

關於代碼本身。你最好的選擇是減少循環次數。這是沒有意義的複雜性,因爲主循環中的每個循環都將運行47次。有關如何解決算法的建議,請查看有關組合/排列的其他問題。對於example