使用递归计算二维金字塔阵列权重

时间:2018-11-19 16:28:12

标签: java recursion

我正在做一些有关递归的作业,而我一直在努力理解如何做一些作业。

作为分配的一部分,我们必须计算每个对象支持的重量。这是物体本身的重量,加上其上方物体承受的重量的一半。

   A

  B C

 D E F

G H I J

所以A本身就是

B本身+ A的一半

C本身+ A的一半

E本身+ B的一半+ C的一半

这些是我的方法应通过的一些测试

public void RecursionTestComputePyramidWeightsInputTest() {
    double [][] weights = {{}};
    double [][] weights1 = {
            { 51.18 },
            { 55.90, 131.25 },
            { 69.05, 133.66, 132.82 },
            { 53.43, 139.61, 134.06, 121.63 }
    };

    Assert.assertEquals("ComputePyramidWeights must be able to handle an empty array", 0.0, Recursion.computePyramidWeights(weights, 0, 0), 0.001);
    Assert.assertEquals("ComputePyramidWeights must be able to handle negative row values", 0.0, Recursion.computePyramidWeights(weights, -1, 0), 0.001);
    Assert.assertEquals("ComputePyramidWeights must be able to handle negative col values", 0.0, Recursion.computePyramidWeights(weights, 0, -1), 0.001);
    Assert.assertEquals("ComputePyramidWeights must be able to handle invalid column", 0.0, Recursion.computePyramidWeights(weights1, 0, 3), 0.001);
    Assert.assertEquals("ComputePyramidWeights must be able to handle invalid row", 0.0, Recursion.computePyramidWeights(weights1, 4, 0), 0.001);
}

@org.junit.Test
public void RecursionTestComputePyramidWeightsTest() {
    double weights[][] = {
                       { 51.18 },
                    { 55.90, 131.25 },
                { 69.05, 133.66, 132.82 },
            { 53.43, 139.61, 134.06, 121.63 }
    };

    Assert.assertEquals("ComputePyramidWeights must be able to handle an array with a single row and col", 51.18, Recursion.computePyramidWeights(weights, 0, 0), 0.001);
    Assert.assertEquals("ComputePyramidWeights must be able to handle an array with multiple rows and one col", 108.327, Recursion.computePyramidWeights(weights, 3, 0), 0.001);
    Assert.assertEquals("ComputePyramidWeights must be able to handle an array with multiple cols and rows", 227.25, Recursion.computePyramidWeights(weights, 3, 3), 0.001);
}

我正在苦苦挣扎的测试是第一个测试的第4个(必须能够处理无效行),第二个测试的第3个(必须能够处理具有多个cols和行的数组)

到目前为止我的代码

public static double computePyramidWeights(double[][] weights, int row, int column){
    if(row < 0 || column < 0 || column > weights[row].length-1){ // ive also tried row > weights[columns].length-1 and it does what i need it to but for some reason when i use both one stops working.
        return 0.0;
    }
    else if (row == 0 && column == 0){
        return weights[0][0];
    }else if (row == 0 || column == 0){
        return weights[row][column] + .5 *(computePyramidWeights(weights, row-1, column)) + .5 * (computePyramidWeights(weights, row, column-1));
    }
    else return 5; // this is a placeholder so i could run the tests, this is where i would need my recursive call, but i really dont know how to even go about this one.
}

1 个答案:

答案 0 :(得分:1)

添加了条件printf("width %d\n", GetDeviceCaps(GetDC(appHWND), HORZRES)); //1920

  

这是物体本身的重量,加上其上方物体承受的重量的一半。

根据您的定义,目前尚不清楚如何计算中间元素的权重。

我假定它是当前元素的总和加上HBITMAP GetScreenBmp(HDC hdc) { int nScreenWidth = 100; int nScreenHeight = 100; HDC hCaptureDC = CreateCompatibleDC(hdc); HBITMAP hBitmap = CreateCompatibleBitmap(hdc, nScreenWidth, nScreenHeight); HGDIOBJ hOld = SelectObject(hCaptureDC, hBitmap); BitBlt(hCaptureDC, 0, 0, nScreenWidth, nScreenHeight, hdc, 0, 0, SRCCOPY | CAPTUREBLT); SelectObject(hCaptureDC, hOld); // always select the previously selected object once done DeleteDC(hCaptureDC); return hBitmap; } int main() { HWND appHWND = FindWindowA(NULL, "Hello World!"); HDC hdc = GetDC(appHWND); HBITMAP hBitmap = GetScreenBmp(hdc); BITMAPINFO MyBMInfo = { 0 }; MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader); // Get the BITMAPINFO structure from the bitmap if (0 == GetDIBits(hdc, hBitmap, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS)){ cout << "error" << endl; } // create the bitmap buffer BYTE* lpPixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage]; // Better do this here - the original bitmap might have BI_BITFILEDS, which makes it // necessary to read the color table - you might not want this. MyBMInfo.bmiHeader.biCompression = BI_RGB; // get the actual bitmap buffer if (0 == GetDIBits(hdc, hBitmap, 0, MyBMInfo.bmiHeader.biHeight, (LPVOID)lpPixels, &MyBMInfo, DIB_RGB_COLORS)) { cout << "error2" << endl; } for (int i = 0; i < 100; i++) { printf("%d\t", (int)lpPixels[i]); } DeleteObject(hBitmap); ReleaseDC(NULL, hdc); delete[] lpPixels; return 0; } 处元素重量的一半,因为这会产生预期的输出。

但是对于第一列,我想您必须获得当前元素+元素重量的一半row > weights.length - 1(同样基于测试输出)

我删除了weights[row - 1][column - 1]的大小写,因为第一行只能包含一个元素,因此第一个weights[row - 1][0]大小写将覆盖此元素。

这是通过所有测试的

row = 0