gpt4 book ai didi

c - 数字的乘积,未打印正确的数字

转载 作者:行者123 更新时间:2023-11-30 18:19:16 26 4
gpt4 key购买 nike

我有一个二维数组(矩阵),我试图计算相邻数字的最大乘积。它与欧拉计划Problem 11非常相似。除非用户在计算中输入他们想要的相邻数字的数量。我想我一切都好。问题是,如果我使用整数来计算 5 个整数 99 的乘积(例如 99*99*99*99*99),它将无法正确显示。可以检查的相邻数字的最大数量是 99。我尝试更改为长 double (如您在代码中看到的),但它打印出荒谬的数字,例如对于 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));
}

最佳答案

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

关于c - 数字的乘积,未打印正确的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9522246/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com