2013-10-05 84 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