如何用java编写n级嵌入循环 [英] how to write n-level embeded loop with java

查看:118
本文介绍了如何用java编写n级嵌入循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定M个整数(N1,N2,Nm),我想编写一个N级嵌入式循环,如下所示:

Given M integers (N1, N2, Nm), I want to write a N-level embedded loop like following :

for (int a = 0; a < N1; a++)
  for (int b = 0; b < N2; b++)
    for (int c = 0; c < N3; c++)
      ....
        for (int m = 0; m < Nm; m++)
          operation

由于M是一个变量,我不能写for循环的固定数字级别。什么技巧可以帮助?

Since M is a variable, I cannot write fixed number level for-loop. What tricks could help ?

推荐答案

与@Andreeas相同的解决方案,只是有更多解释(他发布答案的速度更快,我正在添加我花了一个时间来解释这个问题的理由):

The same solution as @Andreeas', just with more explanations (he was faster in posting the answer, I'm adding mine to give myself a reason for the time I spent with the explanations):

import java.util.Arrays;

public class Multiloop {

  static public void doSomething(int... maxIndexes) {
    // quick check: if any of the maxIndexes is zeo or less
    // there's a snowball in a furnace chance for the most inner loop
    // to get executed
    for(int i=0; i<maxIndexes.length; i++) {
      if(maxIndexes[i]<=0) {
        return; // nothing to do
      }
    }
    // java guarantees all of then are zero after allocation
    int multiIndex[]=new int[maxIndexes.length];

    int currIndexPos=maxIndexes.length-1; // start looping with the last
    do {
      // this happens when the current index reached its correspondent maximum
      // which of course is maxIndexes[currIndexPos]-1
      while(
          currIndexPos>=0 && 
          multiIndex[currIndexPos]>=maxIndexes[currIndexPos]-1
      ) {
        currIndexPos--; // search for the prev one to increment
      }
      if(currIndexPos<0) {
        // all the indexes have reached their max  value, we are done
        break;
      }
      // if it's not the last index, we need to increment the current one
      // and reset to zero all the others following it 
      if(currIndexPos<maxIndexes.length-1) {
        // if not at the max value, then it's safe to increment it
        multiIndex[currIndexPos]++;
        Arrays.fill(multiIndex, currIndexPos+1, maxIndexes.length, 0);
      }
      // and now all multicycle indexes are to their proper value
      // we reset the currIndexPos to the max and the do what we need to do
      currIndexPos=maxIndexes.length-1;

      /// Cut along the dotted lines and place your own code
      /// ✂...✂...✂...✂...✂...✂...✂...✂...✂...✂
      { // the inner-most cycle, using the multicycle indexes as necessary
        // replace it with what you need here
        // **** Just don't screw up the currIndexPos!!! 
        // **** unless you know what you are doing 
        // **** (e.g. breaking any of the "cycles" on the way)
        char nameForIndex='a';
        for(int i=0; i<maxIndexes.length; i++) {
          if(i>0) {
            System.out.print(',');
          }
          System.out.print(nameForIndex+"="+multiIndex[i]);
          nameForIndex++;
        }
        System.out.println();
      }
      // ✂...✂...✂...✂...✂...✂...✂...✂...✂...

      multiIndex[currIndexPos]++;
    } while(true); // the exit condition is handled at the cycle start anyway
  }


  static public void main(String args[]) {
    // a triple cycle with their respective max indexes
    doSomething(2,4,3);
  }
}

输出:

a=0,b=0,c=0
a=0,b=0,c=1
a=0,b=0,c=2
a=0,b=1,c=0
a=0,b=1,c=1
a=0,b=1,c=2
a=0,b=2,c=0
a=0,b=2,c=1
a=0,b=2,c=2
a=0,b=3,c=0
a=0,b=3,c=1
a=0,b=3,c=2
a=1,b=0,c=0
a=1,b=0,c=1
a=1,b=0,c=2
a=1,b=1,c=0
a=1,b=1,c=1
a=1,b=1,c=2
a=1,b=2,c=0
a=1,b=2,c=1
a=1,b=2,c=2
a=1,b=3,c=0
a=1,b=3,c=1
a=1,b=3,c=2

这篇关于如何用java编写n级嵌入循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆