简体   繁体   English

循环i和j,其中i!= j

[英]Loop i and j, where i != j

For example I need to get all combinations of 2-values array with numbers {0, 1, 2} where this two numbers are not the same. 例如,我需要获取带有数字{0,1,2}的2值数组的所有组合,其中这两个数字不相同。 I get 我懂了

0 1
0 2
1 0
1 2
2 0 
2 1

and I ignore 我无视

0 0
1 1
2 2

Now I use 现在我用

  for (int i= 0; i < L ; i++) {
                for (int j = 0; j < L; j++) {
                    if (i!= j) {

but it is very slow? 但这很慢吗? Any solution for this? 有什么解决办法吗? L will be > 4000. L将> 4000。

What am I doing is finding every combinations of splitting matrix to 4 sub-matrices 我正在做的是找到将拆分矩阵划分为4个子矩阵的每个组合

example: 例:

 3 | 0   2  -8  -8
 5 | 3   2   2   3
 2 | 5   2   1   4
 -------------------
 3   4  -1 |  4  2
-3   6   2 |  4  3

and computing their sum using sum-table. 并使用总和表计算总和。

Related question: Split matrix into 4 sub-matrices with lowest difference between their sum 相关问题:将矩阵拆分为4个子矩阵,它们的和之间的差最小

So for matix I have one horizontal line and two vertical and I am computing sum of 4 matrices, but two vertical lines should not crate one big vertical line, so i != j. 因此对于matix,我有一条水平线和两条垂直线,并且我正在计算4个矩阵的总和,但是两条垂直线不应创建一条大的垂直线,因此i!= j。

UPDATE 1 order of the pairs is relevant 对的更新1顺序相关

you could improve the perfomance by: 您可以通过以下方式提高性能:

for (int i= 0; i < L ; i++) {
                for (int j = i + 1 ; j < L; j++) {
System.out.println.(i + " " + j + "\n" + j + " " + i);
}}

You might be able to get some benefit from 也许可以从中受益

for (int i= 0; i < L ; ++i) {
  int j=0;
  for (; j < i; ++j) {
  }
  for (++j; j < L; ++j) {
  }
}

which avoids testing L while you're still testing i . 这避免了在您仍在测试i测试L I've also switched to pre-increment -- since you aren't using the result it probably doesn't make any difference after the optimizer is done with it, but postincrement is, conceptually, a more complicated operation unless there happens to be instruction-set support for it. 我也切换到了预增量-因为您没有使用结果,所以在优化程序完成后它可能没有任何区别,但是从概念上讲,后增量是一个更复杂的操作,除非碰巧存在指令集支持。

If you can change the order in which results are reported, this would be faster: 如果您可以更改报告结果的顺序,则速度会更快:

int L1=L-1;
for (int i=L1; i >=0 ; --i) {
  int j=L1;
  for (; j > i; --j) {
  }
  for (--j; j >=0; --j) {
  }
}

Testing against most values requires a subtraction. 针对大多数值进行测试需要减去。 Testing against zero doesn't; 对零进行测试不会; it occurs "for free" when the value is loaded. 加载值时“免费”发生。 In fact, you could improve it further: 实际上,您可以进一步改进它:

int L1=L-1, i=L1;
do {
  int j=L1;
  do {
  } while(--j>i);
  while(--j>=0)
  {
  }
} while(--i >= 0);

That avoids the redundant initial test. 这样可以避免多余的初始测试。 (Note that you still need to test at the top of the second loop, in case i was 0.) (请注意,如果我为0,您仍然需要在第二个循环的顶部进行测试。)

However, in fact the loop overhead is the least of your problems here; 但是,实际上,循环开销是您在这里遇到的最少问题。 the System.out.println() calls are going to consume far more cycles than the loop control will. System.out.println()调用将比循环控件消耗更多的周期。

If the order of the pairs is relevant (eg you want to consider both {1, 2} and {2, 1} as different), there is way to get a significant speedup at this level. 如果成对的顺序相关(例如,您想将{1, 2}{2, 1}视为不同),则可以在此级别上获得显着的加速。

If the order of the pairs is irrelevant (eg you have considered {1, 2} you can skip {2, 1} ), then @goten's Answer gives you a factor of 2 speedup. 如果对的顺序是无关紧要的(例如,你已经考虑{1, 2}您可以跳过{2, 1}然后@悟天的答案给你2加速的一个因素。 UPDATE the Question has been updated to confirm that the order is relevant ... so @goten's solution is not applicable. 更新问题已更新,以确认订单是否相关...因此@goten的解决方案不适用。

You are unlikely to get a significant speedup by micro-optimizing it; 通过微优化,您不太可能获得显着的加速。 eg turning it into a while loop, etc. The JIT compiler is most likely doing the equivalent optimizations behind the scenes anyway. 例如,将其转换为while循环等。JIT编译器很可能在后台进行等效的优化。

If you want better speedup, you will need to change your overall algorithm so that you don't need to consider so many combinations. 如果要提高速度,则需要更改整体算法,从而无需考虑太多组合。 Whether that is feasible will depend on your problem. 是否可行取决于您的问题。

The other alternative to consider is processing the combinations in parallel. 要考虑的另一种选择是并行处理组合。 However, that only helps if you have multiple cores ... and your problem / algorithm are suitable for parallelization. 但是,这只有在您具有多个内核的情况下才有帮助...并且您的问题/算法适合并行化。

UPDATE - Your problem (per the updated question), looks like it should be suitable for parallelization, though you may need to do some careful tuning to minimize the effects of memory contention in accessing the large shared input array. 更新 -您的问题(针对已更新的问题)看起来应该适合并行化,尽管您可能需要进行一些仔细的调整以最大程度地减少访问大型共享输入数组时内存争用的影响。

for given pole and c is size of pole 对于给定的极点,c是极点的大小

 long[] radky = new long[x];
        long cisloRadku = 0;


    long min = 0;
    int q;
    int radek = 0;

    for (q = 0; q < (x); q++) {
        for (int j = 0; j < x; j++) {
            radky[q] += pole[q][j];

        }

        cisloRadku += radky[q];
    }


    long hodnota1 = 0;
    long hodnota2 = 0;

    for (int i = 0; i < (x - 1); i++) {
        hodnota1 += radky[i];
        hodnota2 = cisloRadku - hodnota1;


        long druheMin = hodnota1 - hodnota2;

        if (druheMin < 0) {
            druheMin = druheMin * (-1);
        }
        if (i == 0) {
            min = druheMin;
        }
        if (min > druheMin) {
            min = druheMin;
            radek = i;
        }
    }

    long[][] poleHorni = new long[radek + 1][x];
    long[][] poleDolni = new long[x - (radek + 1)][x];


    for (int i = 0; i < (radek + 1); i++) {
        for (int j = 0; j < x; j++) {
            poleHorni[i][j] = pole[i][j];

        }

    }


    long[] soucetHornich = new long[x];
    long soucetSlopucuUp = 0;
    for (int k = 0; k < x; k++) {
        for (int j = 0; j < radek + 1; j++) {
            soucetHornich[k] += poleHorni[j][k];
        }

        soucetSlopucuUp += soucetHornich[k];
    }


    long cislo1 = 0;
    long cislo2;
    int sloupec = 0;
    long min2 = 0;
    for (int i = 0; i < (x - 1); i++) {
        cislo1 += soucetHornich[i];
        cislo2 = soucetSlopucuUp - cislo1;

        long druheMin2 = cislo1 - cislo2;


        if (druheMin2 < 0) {
            druheMin2 = druheMin2 * (-1);
        }
        if (i == 0) {
            min2 = druheMin2;
            vysledky[0] = cislo1;
            vysledky[1] = cislo2;
        }
        if (min2 > druheMin2) {
            min2 = druheMin2;
            sloupec = i;
            vysledky[0] = cislo1;
            vysledky[1] = cislo2;
        }
    }


    int a = 0;
    int b = 0;

    for (int i = (radek + 1); i < (x); i++) {
        for (int j = 0; j < x; j++) {
            poleDolni[a][b] = pole[i][j];

            b++;
        }
        b = 0;
        a++;

    }


    long[] soucetDolnich = new long[x];
    long soucetSlopucuDown = 0;

    for (int k = 0; k < x; k++) {
        for (int j = 0; j < (x - (radek + 1)); j++) {
            soucetDolnich[k] += poleDolni[j][k];
        }

        soucetSlopucuDown += soucetDolnich[k];
    }

    long cislo1A = 0;
    long cislo2A;
    long min2A = 0;
    for (int i = 0; i < (x - 1); i++) {
        cislo1A += soucetDolnich[i];
        cislo2A = soucetSlopucuDown - cislo1A;

        long druheMin2A = cislo1A - cislo2A;


        if (druheMin2A < 0) {
            druheMin2A = druheMin2A * (-1);
        }
        if (i == 0) {
            min2A = druheMin2A;
            vysledky[2] = cislo1A;
            vysledky[3] = cislo2A;
        }
        if (min2A > druheMin2A) {
            min2A = druheMin2A;

            vysledky[2] = cislo1A;
            vysledky[3] = cislo2A;
        }



    long[] vyslRozdil = new long[6];
    vyslRozdil[0] = vysledky[0] - vysledky[1];
    vyslRozdil[1] = vysledky[0] - vysledky[2];
    vyslRozdil[2] = vysledky[0] - vysledky[3];
    vyslRozdil[3] = vysledky[1] - vysledky[2];
    vyslRozdil[4] = vysledky[1] - vysledky[3];
    vyslRozdil[5] = vysledky[2] - vysledky[3];
    long max = 0;
    long max2;

    for (int i = 0; i < 6; i++) {
        if (vyslRozdil[i] < 0) {
            vyslRozdil[i] *= -1;
        }
        max2 = vyslRozdil[i];
        if (i == 0) {
            max = max2;
        }
        if (max < max2) {
            max = max2;
        }
    }

    System.out.println(max);

找到对 (i,j) 使得 i <j and (a[i] + a[j]) is maximum< div><div id="text_translate"><p> 给定一个未排序的数组——找到一对 arr[i] 和 arr[j] 使得arr[i] &lt; arr[j] &amp; i&lt;j并且(arr[i] + arr[j])是最大的。</p><p> 预期时间复杂度 - O(n)</p><p> 对于数组a = {4, 1, 3, 2, 5, 3}</p><pre> pair is (4, 5).</pre><p> 这是我试过的代码..</p><pre> void findPair(int[] a){ int n = a.length; int max = a[0]; int secondMax = Integer.MIN_VALUE; for(int i=1; i&lt;n; i++){ if(a[i]&gt;max){ secondMax = max; max = a[i]; } } if(secondMax == Integer.MIN_VALUE){ System.out.println("-1 -1"); } else{ System.out.println(secondMax+" "+max); } }</pre></div></j> - Find the pair (i,j) such that i<j and and (a[i] + a[j]) is maximum

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 j 收缩为 j = (j - 1) &amp; i 的循环的复杂性 - Complexity of a loop where j shrinks as j = (j - 1) & i 查找数组中A [i] * A [j]&gt; A [i] + A [j]的对数 - Finding number of pairs in array where A[i] * A[j] > A[i] + A[j] 如何在n &lt;= 900且a(i,j)= 0或a(i,j)= 1的n * n矩阵中找到每个a(i,j)的条目? - How to find entry of each a(i, j) in n*n matrix where n<=900 and a(i,j)=0 or a(i, j)=1? 如何声明i和j使其成为无限循环? - how to declare i and j to make it be an infinite loop? 在Java中将i的for循环整数j增加 - Increasing a for loop integer j by i in Java 有没有办法遍历 2 个 for 循环,使得 (i+j) &gt; 3 - Is there a way to loop through 2 for loops, such that (i+j) > 3 找到 a[i] 和 a[j] 之间的最大可能差异,其中 0 &lt; i &lt; a.length 和 i &lt; j &lt; a.length - find max possible difference between a[i] and a[j], where 0 < i < a.length and i < j < a.length 为什么(i <= j && j <= i && i!= j)评估为TRUE? - Why does (i<=j && j<=i && i!=j) evaluate to TRUE? 找到对 (i,j) 使得 i <j and (a[i] + a[j]) is maximum< div><div id="text_translate"><p> 给定一个未排序的数组——找到一对 arr[i] 和 arr[j] 使得arr[i] &lt; arr[j] &amp; i&lt;j并且(arr[i] + arr[j])是最大的。</p><p> 预期时间复杂度 - O(n)</p><p> 对于数组a = {4, 1, 3, 2, 5, 3}</p><pre> pair is (4, 5).</pre><p> 这是我试过的代码..</p><pre> void findPair(int[] a){ int n = a.length; int max = a[0]; int secondMax = Integer.MIN_VALUE; for(int i=1; i&lt;n; i++){ if(a[i]&gt;max){ secondMax = max; max = a[i]; } } if(secondMax == Integer.MIN_VALUE){ System.out.println("-1 -1"); } else{ System.out.println(secondMax+" "+max); } }</pre></div></j> - Find the pair (i,j) such that i<j and and (a[i] + a[j]) is maximum 算法的复杂度和效率:a [j] -a [i] i&gt; = j - Complexity and Efficiency in Algorithm for: a[j]-a[i] i>=j
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM