2013-10-05 77 views
0

我有一個關於如何查看這些生成的數組的方法和過程的問題。 基本上我想創建一個[a,b,c,(a + b + c)]的數組,以及[d,e,f,(d + e + f)]的第二個數組,如果array1和array2中的第三個元素是相同的,將數組顯示爲字符串。嵌套for循環和特定數組元素搜索

int num = 10; 
for(int a = 0; a < num; a++){ 
    for(int b = 0; b < num; b++){ 
     for(int c = 0; c < num; c++){ 
     if(a<=b && b<=c){ 
      arrayOne[0] = a; 
      arrayOne[1] = b; 
      arrayOne[2] = c; 
      arrayOne[3] = (a+b+c); 
     } 
     } 
    } 
} 

for(int d = 0; d < num; e++){ 
    for(int e = 0; e < num; e++){ 
     for(int f = 0; f < num; f++){ 
     if(d<=e && e<=f){ 
      arrayTwo[0] = d; 
      arrayTwo[1] = e; 
      arrayTwo[2] = f; 
      arrayTwo[3] = (f -(d+e)); 
     } 
     } 
    } 
} 

,你可以看到我是超出stump.I我不太清楚,我可以得到陣列的每個迭代和每個陣列中的資金匹配和以及顯示相應陣列比較值,他們謝謝大家的進步。

+0

你能舉一個你想要的陣列的樣子嗎?現在看起來您有兩個長度爲4的數組,您只需重複覆蓋這些值,而無需使用它們。 – Maria

+1

現在你正在獨立循環你的'a,b,c',然後循環'd,e,f'。在第一個嵌套'for'完成時,你有'[num,num,num,3 * num]'作爲你的數組。原則上,第二個循環的每次迭代都會生成與第一個循環的相應迭代相同的數據 - 並且嵌套循環將以相同的值退出。不確定你想要達到什麼目的? – Floris

+0

我想創建兩個數組,在他們自己的不同運行時間,最後我想掃描所有數組,我查看是否只有當arrayOne和arrayTwo在每個數組的第三個元素中有相同的總和,然後顯示重要的數組 – Mario

回答

1

如果我正確理解你的問題,如果a=1, b=3, c=4d=2, e=3, f=3你想打印一些東西沿線1 + 3 + 4 = 8 = 2 + 3 + 3。首先,你現在正在做的是創建兩個數組,如評論中描述的Floris。你想要做的是所有的值存儲在陣列中的一個陣列,如下所示:

int max; \\ To determine the value of max see the edit below. 
int array[][] = new int[max][num]; 
int index = 0; 
for (int a=0; a < num; a++) { 
    for (int b=a; b < num; b++) { 
     for (int c=b; c < num; c++) { 
      array[index][0] = a; 
      array[index][1] = b; 
      array[index][2] = c; 
      array[index][3] = a + b + c; 
      index++; 
     } 
    } 
} 

for (int i = 0; i < max; i++) { 
    for (int j = i; j < max; j++) { 
     if (array[i][3] == array[j][3]) { 
      string outString = array[i][0] + " + " + array[i][1] + " + " + array[i][2] + " = " + array[i][3] + " = " + array[j][0] + " + " + array[j][1] + " + " + array[i][2]; 
      System.out.println(outString); 
     } 
    } 
} 

你可以看到,我從b提高性能通過啓動從abc因爲你拋棄所有的值其中b < ac < b。這也應該消除您的if聲明的需要(我說應該只是因爲我沒有測試過)。由於三重嵌套循環的複雜性,我需要使用獨立索引。

編輯2:忽略我。我做了combinatorics錯誤。假設An,k是具有[n]中的元素的長度爲k的無序集合的數量(這將實現你所期望的)。然後An,k = An-1,k + An,k-1。我們知道An,1 = n(因爲這些值是0,1,2,3,4,...,n)和A1,n = 1(因爲唯一的值可以是11111 ... 1 n次)。在這種情況下,我們感興趣的是n= numk = 3,所以在價值觀堵我們得到

A_num,3 = A_num-1,3 + A_num,2 

應用遞歸,直到你來到一個答案的方程式。例如,如果num爲5:

A_5,3 = A_4,3 + A_5,2 
     = A_3,3 + A_4,2 + A_4,2 + A_5,1 
     = A_3,3 + 2(A_4,2) + 5 
     = A_2,3 + A_3,2 + 2(A_3,2) + 2(A_4,1) + 5 
     = A_2,3 + 3(A_3,2) + 2(4) + 5 
     = A_1,3 + A_2,2 + 3(A_2,2) + 3(A_3,1) + 2(4) + 5 
     = 1 + 4(A_2,2) + 3(3) + 2(4) + 5 
     = 1 + 4(A_1,2) + 4(A_2,1) + 3(3) + 2(4) + 5 
     = 1 + 4(1) + 4(2) + 3(3) + 2(4) + 5 
     = 5(1) + 4(2) + 3(3) + 2(4) + 5 

它看起來像這樣可以簡化到(num + (num - 1)(2) + (num - 2)(3) + ... + (2)(num - 1) + num)這是binomial(num, num)但我沒有做的工作肯定地說。

+0

我知道字符串連接是一種混亂。如果您願意,可以將其改爲其他東西。 – Maria

+0

第二個'for'循環中的if條件應該包含== ==而不是'='我猜測,儘管從來沒有測試過它,但是當你需要比較兩個值時,這仍然是賦值:-) +1對於其餘的問題,儘管這個問題對我來說還是有點不清楚。 –

+0

謝謝@nIcEcOw!我根據你的更正進行了修改:) – Maria

1
int givenNumber = 10; 
int []arrayOne = new int [4]; 
int []arrayTwo = new int [4]; 
int count = 0; 

for (int i = 0; i < givenNumber; i ++) 
{  
    for (int x = 0; x < givenNumber; x ++) 
    { 
     for (int a = 0; a < givenNumber; a++){ 
      arrayOne[0] = (int)(a * java.lang.Math.random() + x); 
      arrayOne[1] = (int)(a * java.lang.Math.random() + x); 
      arrayOne[2] = (int)(a * java.lang.Math.random() + x); 
      arrayOne[3] = (int)(arrayOne[0]+arrayOne[1]+arrayOne[2]); 
     } 

     for (int b = 0; b < givenNumber; b++){ 
      arrayTwo[0] = (int)(b * java.lang.Math.random() + x); 
      arrayTwo[1] = (int)(b * java.lang.Math.random() + x); 
      arrayTwo[2] = (int)(b * java.lang.Math.random() + x); 
      arrayTwo[3] = (int)(arrayTwo[0]+arrayTwo[1]+arrayTwo[2]); 
     } 


     if (arrayOne[3] == arrayTwo[3]) 
     { 
      for (int a = 0; a < 2; a++) 
      { 
       System.out.print(arrayOne[a] + " + "); 
      } System.out.print(arrayOne[2] + " = " + arrayOne[3] + " = "); 

      for (int a = 0; a < 2; a++) 
      { 
       System.out.print(arrayTwo[a] + " + "); 
      } System.out.print(arrayTwo[2]);  

      System.out.println("\n"); 
      count += 1; 
     } 
    } 

} 
     if (count == 0) 
      System.out.println(
       "\nOops! you dont have a match...\n" + 
        "Please try running the program again.\n"); 
+0

好的,這似乎工作,但我想改變數學運算。 arrayTwo [3] =(long)Math.pow(a,5)+(long)Math.pow(b,5)+(long)Math.pow(c,5) arrayO [3] (長,長)Math.pow(f,5) - ((long)Math.pow(d,5)+(long)Math.pow(e,5)); – Mario