2017-10-27 34 views
1

私はC++でパスカル三角形を作ろうとしています。しかし、私はそれをすることができませんでした!私のコードに何が問題なのですか?C++でのパスカルの三角形

#include <iostream> 
using namespace std; 

int main() 
{ 
    int rows = 5; 
    int currentNumber = 1; 
    int numbers; 
    int tempNumber; 
    bool odd; 

    for(int i = 0; i < rows; i++) 
    { 
     numbers = i+1; 
     if(i%2 != 0) { 
      odd = true; 
     } else { 
      odd = false; 
     } 
     for(int j = 0; j < (rows/2)-(numbers/2); j++) 
     { 
      cout << " "; 
     } 
     if(odd) 
     { 
      cout << " "; 
     } 

     currentNumber = 1; 
     tempNumber = 0; 
     for(int k = 0; k < numbers; k++) 
     { 
      cout << currentNumber << " "; 

      if(k > numbers/2) { 
       currentNumber-=tempNumber; 
       tempNumber-=currentNumber; 
      } else { 
       currentNumber+=tempNumber; 
       tempNumber+=currentNumber; 
      } 
     } 
     cout << endl; 
    } 

    cout << "test"; 
} 

、出力は以下の通りです:

1 
    1 1 
    1 1 2 
1 1 2 5 
1 1 2 5 -3 
test 

答えて

3

それらが印刷されているとき、私はどうしたらまず最初に、数字の実際の配置を心配されていません。これはプログラミング全体にわたって同様の概念であり、物事がどのように表示されるかの懸念と、物事がデータにどのように含まれているかという「ビュー」と、「モデル」とを分離したいと考えています。 C++はあなたが練習しようとしていることだと主張しているので、クラス構造とオブジェクトを使用します。これは主にオブジェクト指向プログラミング(OOP)のパラダイムに焦点を当てています。これらのアイデアを活用して、三角形の作成方法を考え出すことができます。私たちが達成したい何

は、次のような構造の手続き生産です:

 1 
    1 1 
    1 2 1 
    1 3 3 1 
1 4 6 4 1 

これは我々が本当に作りたい影響を及ぼす達成するために:

1 
1,1 
1,2,1 
1,3,3,1 
1,4,6,4,1 

あなたは上記の図から気付いた場合、私たちは、公理(最初の反復/開始する前にそこにあると想定されるもの)から始まることを推論することができます。この場合は単に数字1だけを含む行です。各生産持っている最初に公理で始まる、1)の上の行に2つの値を追加し、同じインデックスを持ち、次のインデックスを付けることによって、それを生成するルールが与えられます。アルゴリズムのために、我々は、生産が数字を見つけることができない場合、数字は存在するが表示されていないと言い、それは0であると仮定します。左の方には最初の値はありません。右端にある場合は2番目の値はありません)

2番目の図を見ると、最初の行はちょうど1私たちはそれが本当に0,1,0のふりをするつもりです。

0,1,0 
0,1,1,0 
0,1,2,1,0 
0,1,3,3,1,0 
0,1,4,6,4,1,0 

最も重要な部分は、我々は0,1,0であることを前提とします公理1である:それは私たちに多くのようにピラミッドを与えます。ここでは、 "生産"のコード、つまり行を作成するメソッド、その前の行を指定してみましょう。前の行の数字のリストを取得し、次の行を作成する関数を作成したいと考えています。どちらの場合も、0が埋め込まれていると仮定します。その機能は次のようになります。

std::vector<int> produceNextRow(std::vector<int> previousRow) { 
    std::vector<int> nextRow; 

    int left, right = 0; 
    /* For each of the numbers in the previous row, plus one more 
    because each next row has one more value than the last */ 
    for(int i = 0; i < previousRow.size()+1 ; i++) { 

    // If we're all the way on the left we need 'left' to be our imaginary 0 
    if(i == 0) { 
     left = 0; 
    } else { 
     /* Otherwise we want it to be the value in the same position of the 
     previous row which is slightly to the left, hence -1 */ 
     left = previousRow[i-1]; 
    } 

    // If we're all the way on the right, we need 'right' to be our imaginary 0 
    if(i == previousRow.size()) { 
     right = 0; 
    } else { 
    /* Otherwise we want it to be the value of the previous row, at the same 
     position */ 
     right = previousRow[i]; 
    } 

    /* Finally we add left and right, to get our new number, then we put it into 
    the next row */ 
    nextRow.push_back(left+right); 
    } 

    // Let's return our new row we created 
    return nextRow; 
} 

実際にはこれがすべて必要です。今では、間にスペースを入れて印刷するためにトリックを使用するだけです。私はここにいることをしないだろうが、私は三角形の数行を生成するために、この機能を使用する方法を紹介します:

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

    // Vector of vectors of ints! This is our entire triangle 
    std::vector<std::vector<int>> triangle; 
    std::vector<int> axiom {1}; 

    // Lets put our axiom into the triangle first 
    triangle.push_back(axiom); 

    // Alright, for the sake of example lets do some number of productions like 6 
    int productions = 6; 
    for(int i=0; i < productions ; i++) { 
    triangle.push_back(produceNextRow(triangle[i])); 
    } 

    /* Now lets print out our triangle, you'd replace this with something fancy 
    that maybe computs how many spaces are required, and makes the triangle look 
    better. I'll leave that as an excersize for you */ 

    // For each row in the triangle 
    for(int i=0; i < triangle.size() ; i++) { 
    // For each number in the row 
    for(int j=0; j < triangle[i].size() ; j++) { 
     // print that number followed by a comma and a space 
     std::cout << triangle[i][j] << ", "; 
    } 
    // print a new line after each row 
    std::cout << std::endl; 
    } 

    return 0; 
} 

私はクラスオブジェクトにこれを入れて左、ともかなり印刷しましたそれは実際のパスカル三角形のようにあなたのための練習として、私はあなたがそれを試してみて、実際にproduceNextRowメソッドで何が起こっているか考えて時間を費やすと思います。

3

左揃えとして、あなたはそれは2D配列として提示することができる(https://en.wikipedia.org/wiki/Pascal%27s_triangle#Overall_patterns_and_properties)あなたの三角形を想像した場合:

#include <iomanip> //setw 
.... 

    static int const size = 20; 
    int const width = 6; 
    int cell[size][size+1]; 

    for (int i = 0; i < size; ++i) 
    { 
     cell[i][0] = 1; 
     cell[i][i + 1] = 0; 

     std::cout << std::string((size - i) * width/2, 0x20) << cell[i][0]; 

     for (int j = 1; j <= i; ++j) 
     { 
      cell[i][j] = cell[i - 1][j - 1] + cell[i - 1][j]; 
      std::cout << std::setw(width) << cell[i][j]; 
     } 

     std::cout << std::endl; 
    } 

プリント:

enter image description here