2012-03-01 64 views
-1

我有一個2d數組(矩陣),我試圖計算相鄰數字的最大乘積。它與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));  
} 

回答

7

tempcalcProduct初始化,並且既不是maxProduct。它們將在循環中首次包含隨機垃圾值,這會破壞maxProduct結果。

+0

這不能解決問題,溫度仍然for循環「的臨時內分配一個值反正* = matrix [x + i] [y] – Mike 2012-03-01 19:06:06

+1

@Paul'temp * = a'轉換爲'temp = temp * a'。你正在做的是將一個未初始化的值乘以'a',然後將結果存儲在'temp'中。 – Marlon 2012-03-01 19:08:28

+0

@Paul它可能以一個隨機的垃圾值開始。你應該初始化它......'* ='取決於先前的值。 – Stephen 2012-03-01 19:11:10

1

「矩陣的類型爲」 int'陣,但你的scanf()的‘雙打’到它

+0

'%d'匹配一個可選的帶符號的十進制整數,不是double – jfs 2012-03-01 19:27:24