java - 计算 build 房屋所需的乐高积木数量

标签 java

我正在尝试计算 build 一个简单的乐高房子需要多少乐高积木。

我有以下积木:1x2、2x2、2x4。

我需要计算在给定的宽度、长度和高度下 build 乐高房子需要多少 block 砖。

宽度和长度以点为单位给出,如乐高积木板上所示。 高度以乐高积木的高度给出。

例如:2x4 乐高积木是 ((2*4)*2)。 像这样:enter image description here

例如:如果我想 build 一栋房子: 8 点宽 | 7 点长 | 1 block 高

输出应该告诉我我需要:

  • 总点数:56
  • 1x2:2 block 积木
  • 2x2:2 block 积木
  • 2x4:4 block 积木

因此,如果我用给定的砖 block build 乐高房子,它看起来会像这样:enter image description here

到目前为止,我的代码显示了我可以使用多少 block 砖来 build 房子,但我不知道如何让它只显示所需的绝对必要的砖 block 。

public static void main(String[] args) {
    // TODO code application logic here

    Scanner sc = new Scanner(System.in);
    int length = 0;
    int width = 0;
    int height = 0;

    int oneXtwo = (1 * 2);
    int twoXtwo = (2 * 2);
    int twoXfour = (2 * 4);

    System.out.println(oneXtwo + "\t" + twoXtwo + "\t" + twoXfour + "\n");

    int totalDots;
    int oneXtwoTotal;
    int twoXtwoTotal;
    int twoXfourTotal;

    System.out.print("Length: ");
    length = sc.nextInt();
    System.out.print("Width: ");
    width = sc.nextInt();
    System.out.print("Height: ");
    height = sc.nextInt();

    totalDots = (length * width) * height;
    oneXtwoTotal = (((length * width) / oneXtwo) * (height));
    twoXtwoTotal = (((length * width) / twoXtwo) * (height));
    twoXfourTotal = (((length * width) / twoXfour) * (height));

    System.out.println("Total Dots: " + totalDots);
    System.out.println("Total 1x2: " + oneXtwoTotal);
    System.out.println("Total 2x2: " + twoXtwoTotal);
    System.out.println("Total 2x4: " + twoXfourTotal);

最佳答案

这应该相当容易。从第一条边开始,除以最大块的长度,向下舍入。这是您需要的这些 block 的数量。然后你用 do [length] modulo [最长 block 的长度] 来得到你需要的剩余点。现在,您对下一个 block 大小重复该过程,直到对所有 block 都完成了该过程。

现在,您取第二面,减去四个点,然后执行相同的算法。

当然,您需要单独处理宽度和/或高度小于4的房屋的特殊情况。

关于java - 计算 build 房屋所需的乐高积木数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46686944/

相关文章:

java - 嵌入模式时,Tomcat SqlException 中的 H2 被另一个进程锁定

java - Android 的动态文本更新 - ViewRootImpl$CalledFromWrongThreadException

java - 使用 Collections 方法对类中的特定内容进行排序

java - switch case 中的枚举引用

java - java中文件上传如何设置UTF-8?

java - 查看端口问题中的元素[特殊]

java.text.ParseException : Unparseable date: "Wed Jan 11 00:00:00 CET 2012" 异常

java - 有没有办法强制 hadoop 作业查看下划线文件夹?

java - 这段代码是否意味着我创建了 10000 个线程来运行该函数?

java - jdbc javax.naming.NameNotFoundException 异常