2008-11-17 76 views
0

我試圖解決歐拉問題18 - >http://projecteuler.net/index.php?section=problems&id=18三角問題

我想用C++做這++(我再學習,並做出了良好的學習歐拉問題/搜索材料)

#include <iostream> 

using namespace std; 

long long unsigned countNums(short,short,short array[][15],short,bool,bool); 

int main(int argc,char **argv) { 

    long long unsigned max = 0; 
    long long unsigned sum; 


    short piramide[][15] = {{75,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
          {95,64,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
          {17,47,82,0,0,0,0,0,0,0,0,0,0,0,0}, 
          {18,35,87,10,0,0,0,0,0,0,0,0,0,0,0}, 
          {20,4,82,47,65,0,0,0,0,0,0,0,0,0,0}, 
          {19,1,23,75,3,34,0,0,0,0,0,0,0,0,0}, 
          {88,2,77,73,7,63,67,0,0,0,0,0,0,0,0}, 
          {99,65,4 ,28,6,16,70,92,0,0,0,0,0,0,0}, 
          {41,41,26,56,83,40,80,70,33,0,0,0,0,0,0}, 
          {41,48,72,33,47,32,37,16,94,29,0,0,0,0,0}, 
          {53,71,44,65,25,43,91,52,97,51,14,0,0,0,0}, 
          {70,11,33,28,77,73,17,78,39,68,17,57,0,0,0}, 
          {91,71,52,38,17,14,91,43,58,50,27,29,48,0,0}, 
          {63,66,4,68,89,53,67,30,73,16,69,87,40,31,0}, 
          {4,62,98,27,23,9,70,98,73,93,38,53,60,4,23}}; 

    for (short i = 0;i<15;i++) { 
     for (short m=0;m<15;m++) { 
      if (piramide[i][m] == 0) 
       break; 
      sum = countNums(i,m,piramide,15,true,true); 
      if (sum > max) 
       max = sum; 
      sum = countNums(i,m,piramide,15,true,false); 
      if (sum > max) 
       max = sum; 
      sum = countNums(i,m,piramide,15,false,true); 
      if (sum > max) 
       max = sum; 
      sum = countNums(i,m,piramide,15,false,false); 
      if (sum > max) 
       max = sum; 

     } 

    } 
    cout << max; 
    return 0; 
} 


long long unsigned countNums(short start_x,short start_y,short array[][15],short size, bool goright,bool goright2) { 
    long long unsigned currentSum; 

    currentSum = array[start_x][start_y]; 

    if (goright) { //go right 
     if ((start_x + 1) < size) 
      start_x++; 
     if ((start_y + 1) < size) 
      start_y++; 
    } 
    else //go down 
     if ((start_x + 1) < size) 
      start_x++; 

    if (goright2) { //still going right 
     for (short i = start_x, m = start_y;i< size && m < size;i++,m++) { 
      currentSum += array[i][m];   
     } 
    } 
    else { //still going down 
     for (short i = start_x;i<size;i++) { 
      currentSum += array[i][start_y];    
     } 
    } 

    return currentSum; 
} 

countNums函數用於向下或對角線。 我已經測試這個功能,像這樣:

short a = 0; 
short b = 0; 
cout << countNums(a,b,piramide,15,true,true) << endl; 
cout << countNums(a,b,piramide,15,true,false) << endl; 
cout << countNums(a,b,piramide,15,false,true) << endl; 
cout << countNums(a,b,piramide,15,false,false) << endl; 
return 0; 

而且它的工作(我也改變了功能一點,所以它會打印每一個數字它正在經歷)

但我還是不明白正確的結果。這下降並且向右並且仍然向右(相鄰的數字向右),下降並且繼續向下(與左邊相鄰的數字)。 我在這裏做錯了什麼,有人嗎?


阿拉斯泰爾:它的簡單

長長無符號countNums(短start_x,短start_y,短陣列[] [15],短尺寸,布爾goright,布爾goright2);

start_x和start_y是陣列的COORDS 數組是引用數組 大小的數組的只是大小(它總是15) goright是知道如果我要下去,右或只是下來 goright2是要知道我是否會繼續下去或走向左邊

回答

3

好吧,所以首先,我有點不清楚你認爲問題是什麼。我無法解析所有的第二句話......

其次,您可能想在這裏重新考慮您的設計。考慮執行單個離散任務的函數,而不是與應用程序的其他部分交織在一起(例如,閱讀「緊密耦合和鬆散綁定」)。對我來說,這裏的一個大警告是存在一個泛型函數名稱countNums以及一長串參數。

我認爲如果要將問題分成更小,更容易理解的塊,則可能會發現問題更容易找到。我知道我會採取的方法,但我假設整個練習的重點是幫助你練習編程技巧,所以我只是將它留在那...

0

我有點困惑的問題..
我會開始清理你的代碼。

long long unsigned countNums(short x, 
          short y, 
          short array[][15], 
          short size, 
          bool goright, 
          bool goright2) 
{ 
    long long unsigned currentSum; 
    currentSum = array[x][y]; 

    if ((x + 1) < size) x++; //this happened in both your if cases 

    if (goright && ((y + 1) < size)  y++; 

    if (goright2) 
    { 
     for (;x< size && y< size;x++,y++) 
      currentSum += array[x][y];   

    } 
    else 
    { 
     for (;x<size;x++) 
      currentSum += array[x][y];    
    } 
    return currentSum; 
} 

現在我看了這個問題,我不確定它是在做你想做的。 既然這不是你想要的,我會建議psudo-首先編碼的答案。 忘記代碼.. sudo代碼中的答案是什麼。
哦,爲了所有聖潔的愛。不要在for循環中放置多個初始化器。我知道我會爲此而眩暈,但這很混亂,真的不需要。 你可能會考慮的是一個recusive功能。似乎理想的這個問題。

+0

int j = 1; for(int i = 0; i baash05 2008-11-17 01:41:45

0

主函數檢查一個條目不是零,但它調用的另一個函數不會檢查它,即使它再次更改索引。我不知道,我真的不明白你在做什麼。

我看到15個元素的數組大小15,聲明的索引是否也會從0開始?讓我檢查一下。只要確定,聲明大小但訪問基於0.好。

爲什麼你以後使用一個嵌套的for語句而不是一個compunded-condition語句?更好地檢查&&沒有比<更高的優先級。第8組擊敗了第13組12 here。遞增運算符在聲明中不多次使用,這很好。

這聽起來像你之前用另一種語言做過這個問題,你能否也跟蹤一下,找到你的新程序第一個不同的地方?

其他人是對的,問題很混亂。我會首先嚐試解決問題,如果金字塔從那時開始建立起來,從另一個矩陣中建立分數,得到最高分,然後從底部開始到頂部。

1

我已經解決了這個問題。鑑於歐拉項目的本質就是要自己解決這個問題(「複製一個解決的縱橫字謎並不意味着你解決了這個問題」),而且我不想爲了別人而毀了這個,我真正能夠說的是你的解決方案看起來過於複雜。

但是,您可以在閱讀文件時解決此問題。以這種方式解決問題#69將是一件輕而易舉的事情!

祝你好運!