数字的产品,不打印正确的数字

时间:2012-03-01 18:58:06

标签: c arrays product

我有一个二维数组(矩阵),我试图计算相邻数字的最大乘积。它与Project Euler Problem 11非常相似,只是用户在计算中输入了他们想要的相邻数字。我觉得我没事。问题是如果我使用整数来计算99个整数的乘积(例如99 * 99 * 99 * 99 * 99),它将无法正确显示。可以检查的相邻数字的最大数量是99.我已经尝试过更改为长双打(正如您在代码中看到的那样),但是它会打印出荒谬的数字,例如有3个相邻的数字我在所有矩阵位置输入99并应该返回970299(我在maxProduct为int时执行),但我得到:

-497917511184158537131936752181264370659584929560826523880745083032965215342755650440802286656251727430041200624372430370294634536699364412350122489510814753628581807006780156992324264734484592980976635224618682514265787653963930812412392499329499188301075222828863209569131692032

我觉得这很明显,但我看不到它。

#include <stdio.h>
#include <stdlib.h>

long double calcProduct(int n, int m, int ** matrix)
{
    int i, x, y; //Loop counters
    long double maxProduct; //Used to hold the maximum product of numbers found so far
    long double temp; //Used to hold the current product of numbers

    //Searching left to right
    for(y = 0; y < n; y++)
    {
        for(x = 0; x <= n - m; x++)
        {
            for(i = 0; i < m; i++)
            {
                temp *= matrix[x + i][y];
            }

            if(temp > maxProduct)
            {
                maxProduct = temp;
            }

        temp = 1;
        }
    }

    //Searching top down
    for(x = 0; x < n; x++)
    {
        for(y = 0; y <= n - m; y++)
        {
            for(i = 0; i < m; i++)
            {
                temp *= matrix[x][y + i];
            }

            if(temp > maxProduct)
            {
                maxProduct = temp;
            }

        temp = 1;
        }
    }

    temp = 1;

    //Searching diagonal down right
    for(x = 0; x < n - m; x++)
    {
        for(y = 0; y <= n - m; y++)
        {
            for(i = 0; i < m; i++)
            {
                temp *= matrix[x + i][y + i];
            }

            if(temp > maxProduct)
            {
                maxProduct = temp;  
            }

        temp = 1;
        }
    }

    temp = 1;

    //Searching diagonal up right
    for(x = 0; x < n - m; x++)
    {
        for(y = n - 1; y >= m - 1; y--)
        {
            for(i = 0; i < m; i++)
            {
                temp *= matrix[x + i][y - i];
            }

            if(temp > maxProduct)
            {
                maxProduct = temp;  
            }

        temp = 1;
        }
    }

    return maxProduct;
}

main()
{
    int ** matrix; //2D array to hold the matrix items
    int n, m; //Used to hold the size of the matrix (n) and the number of adjacent numbers to include in the calculation (m)
    int i, j; //Loop counters

    //Taking input of n (for size of grid) and m (number of adjacent numbers to include in calculation)

    scanf("%d %d", &n, &m);


    //Assign the array 'matrix' with the size of int multiplied by the number of items to hold (n)
    matrix = (int **)malloc(sizeof(int*)*n);
    //If the matrix is null then exit the program
    if (matrix == NULL) 
    {
        exit (0);
    }

    for(i = 0; i < n; i++)
    {
        matrix[i] = (int *)malloc(sizeof(int)*n);
        if(matrix[i] == NULL) exit (0);
    }

    //Getting the numbers which are held in the matrix
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < n; j++)
        {
            scanf("%d", &matrix[i][j]);
        }
    }

    //Prints the highest product by calling the method calcProduct, giving it n, m and the array matrix
    printf("%.0Lf", calcProduct(n, m, matrix));     
}

2 个答案:

答案 0 :(得分:7)

temp未在calcProduct中初始化,maxProduct也未初始化。它们将首次包含循环中的随机垃圾值,这会破坏您的maxProduct结果。

答案 1 :(得分:1)

'矩阵的类型为'int'-Array,但你的scanf()'加倍'到其中