RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 574559
Accepted
CodeEnjoyer
CodeEnjoyer
Asked:2020-10-06 21:21:07 +0000 UTC2020-10-06 21:21:07 +0000 UTC 2020-10-06 21:21:07 +0000 UTC

如何优化我的代码?

  • 772

我是 Java 新手(使用 Pascal 编程),我需要编写一个程序来解决以下问题(类似于数字谜题):

AB + CC = DC
CE * FB = CEB
FF * GC = GHC
AB - CE = FF
CC + FB = GC 
DC + CEB = GHC

附言 每个字符都是一个数字(从 0 到 9)。每个数字只能使用一次。

所以我写了下面的程序:

public class javaapplication1 {

    public static void main(String[] args) {
        boolean bb=false;
        for (int h = 0; h < 10; h++) {
            for (int g = 0; g < 10; g++) {
                if (bb) {
                    break;
                }
                for (int f = 0; f < 10; f++) {
                    if (bb) {
                        break;
                    }
                    for (int e = 0; e < 10; e++) {
                        if (bb) {
                            break;
                        }
                        for (int d = 0; d < 10; d++) {
                            if (bb) {
                                break;
                            }
                            for (int c = 0; c < 10; c++) {
                                if (bb) {
                                    break;
                                }
                                for (int b = 0; b < 10; b++) {
                                    if (bb) {
                                        break;
                                    }
                                    for (int a = 0; a < 10; a++) {
                                        if (a*10 + b + c*10 + c == d*10 + c) {
                                            if ((c*10+e)*(f*10+b)==(c*100+e*10+b)) {
                                                if ((f*10+f)*(g*10+c)==g*100+h*10+c) {
                                                    if ((a*10+b)-(c*10+e) == f*10+f) {
                                                        if ((c*10+c)+(f*10+b) == g*10+c) {
                                                            if ((d*10+c)+(c*100+e*10+b) == g*100+h*10+c) {
                                                                if (a!=b && a!=c && a!=d && a!=e && a!=f && a!=g && a!=h) {
                                                                    if (b!=c && b!=d && b!=e && b!=f && b!=g && b!=h) {
                                                                        if (c!=d && c!=e && c!=f && c!=g && c!=h) {
                                                                            if (d!=e && d!=f && d!=g && d!=h) {
                                                                                if (e!=f && e!=g && e!=h) {
                                                                                    if (f!=g && f!=h) {
                                                                                        if (g!=h) {
                                                                                            bb = true;
                                                                                            System.out.println(a);
                                                                                            System.out.println(b);
                                                                                            System.out.println(c);
                                                                                            System.out.println(d);
                                                                                            System.out.println(e);
                                                                                            System.out.println(f);
                                                                                            System.out.println(g);
                                                                                            System.out.println(h);
                                                                                            break;
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

它工作并产生正确的结果,但我确信它拼写错误,因为解决方案本身是愚蠢的 - 纯粹的贿赂。请告诉我如何优化这个程序。Z.Y. 为了让我自己的代码更清晰,我在更大程度上写了大量的 if-s。

java
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Nick Volynkin
    2020-10-06T22:51:33Z2020-10-06T22:51:33Z
    1. 无需break通过布尔变量进行复杂化。够return了,它会打破所有循环。
    2. 必须在枚举阶段而不是在最后检查条件“每个数字只能使用一次”。所以你只有10!/2!=10*9*8*7*6*5*4*3支票,没有10^8。
      1. 定义所有数字的集合。
      2. 在外循环中迭代这个集合。
      3. 在下一个循环中,迭代上一个循环中的集合,但不包含在上一个循环中选择的数字。
      4. 在下一个循环中,迭代上一个循环中的集合,而不是在上一个循环中选择的数字......等等。
    3. 条件可以隔离并存储为方法列表 (lambda),然后在循环中检查。
    • 4
  2. Alex Chermenin
    2020-10-06T23:27:27Z2020-10-06T23:27:27Z

    “只是为了好玩”的解决方案,但考虑到并行化,在性能方面甚至更好:

    IntFunction<Integer> a = x -> x / 10000000 % 10;
    IntFunction<Integer> b = x -> x / 1000000 % 10;
    IntFunction<Integer> c = x -> x / 100000 % 10;
    IntFunction<Integer> d = x -> x / 10000 % 10;
    IntFunction<Integer> e = x -> x / 1000 % 10;
    IntFunction<Integer> f = x -> x / 100 % 10;
    IntFunction<Integer> g = x -> x / 10 % 10;
    IntFunction<Integer> h = x -> x % 10;
    
    IntFunction<IntStream> fx =
        i -> IntStream.range(0, 10).filter(x -> {
            if (x == i) return false;
            for (int j = i; j > 0; j /= 10) {
                if (x == j % 10) {
                    return false;
                }
            }
            return true;
        }).map(x -> 10 * i + x);
    
    IntStream.range(0, 10).parallel()
        .flatMap(fx)
        .flatMap(fx)
        .flatMap(fx)
        .flatMap(fx)
        .flatMap(fx)
        .flatMap(fx)
        .flatMap(fx)
        .filter(x -> a.apply(x) * 10 + b.apply(x) + c.apply(x) * 10 + c.apply(x) == d.apply(x) * 10 + c.apply(x))
        .filter(x -> (f.apply(x) * 10 + f.apply(x)) * (g.apply(x) * 10 + c.apply(x)) == g.apply(x) * 100 + h.apply(x) * 10 + c.apply(x))
        .filter(x -> (a.apply(x) * 10 + b.apply(x)) - (c.apply(x) * 10 + e.apply(x)) == f.apply(x) * 10 + f.apply(x))
        .filter(x -> (c.apply(x) * 10 + e.apply(x)) * (f.apply(x) * 10 + b.apply(x)) == (c.apply(x) * 100 + e.apply(x) * 10 + b.apply(x)))
        .filter(x -> (c.apply(x) * 10 + c.apply(x)) + (f.apply(x) * 10 + b.apply(x)) == g.apply(x) * 10 + c.apply(x))
        .filter(x -> (d.apply(x) * 10 + c.apply(x)) + (c.apply(x) * 100 + e.apply(x) * 10 + b.apply(x)) == g.apply(x) * 100 + h.apply(x) * 10 + c.apply(x))
        .forEach(System.out::println);
    
    • 4

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5