如果一个循环中的条件如何重新组合?

时间:2014-12-25 23:21:20

标签: java algorithm loops if-statement

我一直试图循环这些但如果我没有得到它,我在哪里放置循环?

public static void Run(int n) {
    int l;
    int c;
    for (l = 1; l <= 2 * n - 1; l++) {
    System.out.println("\n");
    for (c = 1; c <= 2 * n - 1; c++) {

        if ((l == 1) || (l == 2 * n - 1) || (c == 1) || (c == 2 * n - 1)) {
        System.out.print('a');
        } else if ((l == 2) || (l == 2 * n - 2) || (c == 2) || (c == 2 * n - 2)) {
        System.out.print(defLettre(n, 1));
        } else if ((l == 3) || (l == 2 * n - 3) || (c == 3) || (c == 2 * n - 3)) {
        System.out.print(defLettre(n, 2));
        } else if ((l == 4) || (l == 2 * n - 4) || (c == 4) || (c == 2 * n - 4)) {
        System.out.print(defLettre(n, 3));
        } else if ((l == 5) || (l == 2 * n - 5) || (c == 5) || (c == 2 * n - 5)) {
        System.out.print(defLettre(n, 4));

        } else {
        System.out.print(" ");
        }

    }

    }

}

}

事情是,如果我必须输入的话,n就越多,我就不知道如何重新组合它们。

编辑:

感谢您的回答。我想做的计划是:

http://pastebin.com/dKBGjVqj(无法在此正确粘贴)。

我能够做到这一点,只是令人困惑,因为如果n是10,我会输入10 if..else。

顺便说一句,我的程序需要在1ghz的计算机上以低于1秒的速度运行,并且必须低于8000 Ko。我怎样才能看到1s以下的跑步?我猜.java大小是为了这个大小。

4 个答案:

答案 0 :(得分:2)

使用Java 8,我会做类似的事情:

public static void Run(int n) {
    int l;
    int c;
    for (l = 1; l <= 2 * n - 1; l++) {
        System.out.println("\n");
        for (c = 1; c <= 2 * n - 1; c++) {
            final int lf = l, cf = c;
            IntPredicate pred = x -> lf == x || lf == 2*n - x || cf == x || cf == 2*n - x;
            IntStream.range(1,2*n - 1).filter(pred).findFirst()
               .ifPresent(x -> System.out.println(x == 1 ? "a" : defLettre(n,x)));
        }
    }
}

答案 1 :(得分:2)

你应该解释你想要做的事情的逻辑但是从代码判断,把下面的片段放在第二个循环中(伪代码)

    if(l==c||l+c==2*n)
    {
        int value=min(l,c);
        if(value==1)print("a");
        else print(defLettre(n,value-1));
    }

答案 2 :(得分:0)

您可以尝试以下代码:

public static void Run(int n) {
        int l;
        int c;
        for (l = 1; l <= 2 * n - 1; l++) {
            System.out.println("\n");
            for (c = 1; c <= 2 * n - 1; c++) {
                for(int k=1; k<=n; k++){

                if ((l == k) || (l == 2 * n - k) || (c == k) || (c == 2 * n - k)) {
                    System.out.print('a');
                }else {
                    System.out.print(" ");
                }
                }

            }

        }

    }

答案 3 :(得分:0)

正如其他人所说,我并不完全确定你的目的是什么。但鉴于您的要求,我相信这个解决方案对您有用。

问题

要重述您的问题,您希望只要满足以下四个条件之一就执行defLettre功能。

    某些j
  • l == j
  • 某些j
  • c == j
  • 某些j
  • l == 2 * n - j
  • 某些j
  • c == 2 * n - j

针对l == 1c == 1

的案例的特殊情况

解决方案

为了解决这个问题,你需要使用一个内循环来迭代我在讨论中介绍的j变量。

    public static void Run(int n) {
        final int bound = 2 * n;
        for (int l = 1; l <= bound - 1; l++) {
            System.out.println("\n");
            for (int c = 1; c <= bound - 1; c++) {
                boolean match = false;
                for (int j = 1; j < 2 * n; j++){
                    if ((l == 1) || (l == bound - j) || (c == 1) || (c == bound - j)) {
                        if (j == 1) {
                            System.out.print('a');
                        } else {
                            System.out.print(defLettre(n, j - 1));
                        }
                        match = true;
                        break;
                    }
                }
                if (!match){
                    System.out.println(" ");
                }

            }

        }
    }

讨论

注意,我已将bound定义为2 * n。此外,j之上的最内层循环必须介于j == 1j == 2 * n之间,因为在边界的任何一侧,条件将始终评估为false。

注意事项

您描述的逻辑集成了某些内容,我将其定义为j。但是失败条件会通过打印字符来产生副作用。在j上可能有更好,更严格的界限,这与我想要的不同,但如果没有更好的算法描述,我不知道它们是什么。