【问题标题】:How do I reunite those if conditions in one loop? [closed]如何在一个循环中重新组合这些 if 条件? [关闭]
【发布时间】:2015-02-23 10:25:19
【问题描述】:

我一直在尝试循环这些 if 但我不明白,我必须将循环放在哪里?

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 计算机上以 1s 的速度运行,并且必须低于 8000 Ko。我如何查看 1 秒以下的运行部分?我猜 .java 的大小是为了大小。

【问题讨论】:

标签: java algorithm loops if-statement


【解决方案1】:

使用 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)));
        }
    }
}

【讨论】:

  • 您在 OP 问题中缺少\n
  • 我想你想要IntStream.of(2*n)。否则你不会用正确的参数调用defLettre,即defLettre(n, 4)。此外,对于 x 的值,您的谓词需要在 [1, 2*n] 上进行操作。
  • @isomarcte 好的,谢谢。但是,这段代码的最大问题是它“蛮力”(像其他解决方案一样)所有可能的测试来找到匹配项,而我认为可以避免它。更具体地说,对于我的回答,如果 n 很大,它将创建很多 Predicate 对象,这可能会很昂贵
  • @isomarcte IntStream.of(1).filter(pred).findFirst() 这个表达式将找到第一个匹配,它必然出现在 1 和 2n 之间,因为 lfcf 介于 1 和 2n - 1 之间。实际上,我应该删除第二个else声明
  • 顺便说一句,我喜欢你使用 Java 功能工具。很高兴看到有人使用它们,它们在 Java 8 中编写了更好的代码。我明白你所说的创建许多谓词对象。可惜不能创建惰性求值 Java 或第一类函数,那么您可以避免成本问题。
【解决方案2】:

正如其他人所说,我不完全确定您要做什么。但考虑到您的要求,我相信这个解决方案会为您工作。

问题

为了重申您的问题,您希望在满足四个条件之一时执行defLettre 函数。

  • l == j 对于一些 j
  • c == j 对于一些 j
  • l == 2 * n - j 对于一些 j
  • c == 2 * n - j 对于一些 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 上可能有更好、更严格的界限,与我描述的您想要的不同,但如果没有更好地描述您的算法,我不知道它们是什么。

【讨论】:

  • 找到匹配项需要退出最内层循环。否则,我认为您的解决方案与我的解决方案相同,但代码更长
  • 我以为您只是想简化您的 if else 块。你没有在你的问题中提到任何退出条件。您希望程序在什么情况下退出?
  • 这不是我的问题,我只是发布了一个答案。我的意思是,您的代码可能会为一对 (l,c) 的单个值打印多次,这不是基于 OP 代码的预期行为。因此,您需要在最内部的循环中添加一个中断
  • 哦,对不起,我以为你是海报。我明白你的意思...我会更正一下。
  • 哦,如果没有找到匹配项,您只需在循环结束时打印" "
【解决方案3】:

你应该解释你正在尝试做的事情的逻辑,但是从代码来看,将下面的 sn-p 放在第二个循环中(伪代码)

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

【讨论】:

  • 你的条件不正确
  • @Dici 你应该详细说明这一点。
  • @Carcigenicate 很好。 l 在 OP 的条件下不一定等于 c,因为它是一个析取,所以当条件为真时,l == xc == x 可能不会同时为真。此外,如果我们有l == c,那么l + c 将是2*x4*n - 2 而不是2*n
【解决方案4】:

你可以试试这个代码:

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(" ");
                }
                }

            }

        }

    }

【讨论】:

    猜你喜欢
    • 2022-11-19
    • 2012-09-26
    • 1970-01-01
    • 2015-05-09
    • 2021-10-01
    • 2017-02-02
    • 1970-01-01
    • 2022-01-04
    • 2021-06-30
    相关资源
    最近更新 更多