Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中计算瓷砖地板需要多少瓷砖_Java - Fatal编程技术网

如何在Java中计算瓷砖地板需要多少瓷砖

如何在Java中计算瓷砖地板需要多少瓷砖,java,Java,我最近刚开始编程,遇到了这个问题 如何计算瓷砖地板需要多少瓷砖? 瓷砖为宽度为20的正方形,地板长度和宽度由用户输入 我可以用%来计算我需要多少个完整的瓷砖,但一旦我找到侧面的瓷砖,我就卡住了。切割成更小的条带是允许的,你不能做马赛克 我试着将大小存储在一个列表中,并将它们相加到尽可能接近20。但后来我遇到了这样的事情,比如用一块瓷砖做2块8,用2块瓷砖做2块12,而2块瓷砖就足够了 我似乎找不到一种方法来编码这种逻辑 final int tile = 20; double length

我最近刚开始编程,遇到了这个问题

如何计算瓷砖地板需要多少瓷砖? 瓷砖为宽度为20的正方形,地板长度和宽度由用户输入

我可以用%来计算我需要多少个完整的瓷砖,但一旦我找到侧面的瓷砖,我就卡住了。切割成更小的条带是允许的,你不能做马赛克

我试着将大小存储在一个列表中,并将它们相加到尽可能接近20。但后来我遇到了这样的事情,比如用一块瓷砖做2块8,用2块瓷砖做2块12,而2块瓷砖就足够了

我似乎找不到一种方法来编码这种逻辑

final int tile = 20;
    double length = Input.readDouble();
    double width = Input.readDouble();

    int completeColumTiles = (int)width/tile;
    int completeRowTiles = (int)length/tile;
    int tilesWithoutRest = completeRowTiles * completeColumTiles;

    double restLength = length%tile;
    double restWidth = width%tile;

    int tilesRest = calculateRest(completeColumTiles, completeRowTiles, restLength, restWidth);
    int tilesWithRest = tilesWithoutRest + tilesRest;

    System.out.println("You need " + tilesWithRest + " tiles.");         

因此,基本上我需要计算机测试方面的帮助,那么获得这样的东西怎么样

基本上,假设
private static final int TILE_DIMENSION=20,您需要:

private int getTilesCount(final int floorWidth, final int floorHeight) {
    final int horizontalTiles = (floorWidth % TILE_DIMENSION) + ((floorWidth / TILE_DIMENSION) == 0) ? 1 : 0;
    final int verticalTiles = (floorHeight % TILE_DIMENSION) + ((floorHeight / TILE_DIMENSION) == 0) ? 1 : 0;
    return horizontalTiles * verticalTiles;
}
我在这个方法中所做的可以简单地分解如下:

  • 计算每行中的分幅数(
    水平分幅
  • 计算每列中的分幅数(
    垂直分幅
    ),以及
  • 返回这两个值的乘积
下面是我如何计算每行/每列中瓷砖的数量:

floorWidth / TILE_DIMENSION
+ ((floorWidth / TILE_DIMENSION) == 0) ? 1 : 0;
计算单个行/列中可容纳的平铺数:

floorWidth / TILE_DIMENSION
+ ((floorWidth / TILE_DIMENSION) == 0) ? 1 : 0;
如果该行/列中还有一些空白,请再添加1个磁贴:

floorWidth / TILE_DIMENSION
+ ((floorWidth / TILE_DIMENSION) == 0) ? 1 : 0;

作为旁注,如果您还不知道,我在这里使用的语法(
(evalutaion)?expression1:expression2;
称为a。
它完全对应于doing(在伪代码中):


也许这是可行的:首先计算未切割的块,然后计算间隙最大的一侧,最后计算间隙较小的一侧,尝试重用第一一侧的剩余块

int countTiles() {
    int wholeTilesX = roomWidh / 20;
    int wholeTilesY = roomHeight / 20;

    int remainingGapX = roomWidh % 20;
    int remainingGapY = roomHeight % 20;

    int neededExtraTiles;
    if(remainingGapX > remainingGapY) {
        neededExtraTiles = countTilesNeededForSides(
            wholeTilesX, remainingGapX, wholeTilesY, remainingGapY);
    } else {
        neededExtraTiles = countTilesNeededForSides(
            wholeTilesY, remainingGapY, wholeTilesX, remainingGapX);
    }

    int neededTiles = wholeTilesX*wholeTilesY+neededExtraTiles;
    return neededTiles;
}

// Assuming side A has a larger gap.
private int countTilesNeededForSides(
            int wholeTilesSideA, int gapSideA, 
            int wholeTilesSideB, int gapSideB) {

    int gapPiecesB = wholeTilesSideB * 20/gapSideA;
    int leftoversFittingA = wholeTilesSideB * ((20%gapSideA) / gapSideB);

    // +1 is for the corner
    int neededBitsSideA = (wholeTilesSideA+1) - leftoversFittingA;

    int gapPiecesA = neededBitsSideA > 0 ? neededBitsSideA * 20/gapSideA : 0;   
    return gapPiecesA+gapPiecesB;
}

请发布您的代码。单击并在底部添加。这是我目前拥有的,但我需要使用右侧和底部的瓷砖块来尝试使用较少的瓷砖。因此,基本上,您可以使用1块瓷砖制作2块或更多瓷砖,这取决于右侧和底部的瓷砖块有多大。哎呀。好吧。我不明白:我的坏!