Print Yang Hui's triangle (Pascal ' s triangle)--using chain-type queues

Source: Internet
Author: User
Tags class definition
print Yang Hui's triangle (Pascal ' s triangle)--using chain-type queues 1. The concept of the Yang Hui's triangle Yang Hui's triangle, also called Jia Xian triangle, Pascal Triangle, is a geometric arrangement of two-item coefficients in a triangle. The two-item (A+B) I (A+B) ^i is expanded, and its coefficients constitute the Yang Hui's triangle (Pascal's triangle abroad), which is printed in the first n rows of the expansion coefficients. From the shape of the triangle, in addition to line 1th, when the first row is printed, the data used on the previous line (the I-1 line) is used to print the first line of i+1, and the data in row I is applied. Note: In Europe, this table is called the Pascal Triangle. Pascal (1623--1662) found this rule in 1654, 393 years later than Yang Hui's, 600 years later than Jia Xian. 2. Basic nature of the Yang Hui's triangle The prerequisite is that the number of endpoints is 1. each number equals the sum of two digits above it. each line of the number is symmetrical, starting from 1 to become larger. the number of rows in nth has n+1 items. Nth line number and 2n−1 2^{n-1}. the number of m in the nth row can be expressed as C (N-1,m-1), which is the number of combinations of m-1 elements from n-1 different elements. the number of M of the nth row and the number of n-m+1 are equal, which is one of the properties of the combination number. each number equals the sum of the left and right two digits of the previous line. This property can be used to write out the entire Yang Hui's triangle. That is, the number of n+1 in line I is equal to the sum of the number of i-1 and number of the first n rows, which is also one of the properties of the combinatorial number. That is, C (n+1,i) =c (n,i) +c (n,i-1). The coefficients in the expansion of the (A+B) n (a+b) ^n correspond in turn to each of the items in the Yang Hui's triangle (n+1) row. Number 1th of the 2n+1 line, followed by number 3rd of 2n+2, number 5th of line 2n+3 ... And the number of these numbers is the number of Fibonacci numbers of the first 4n+1, and the 2nd number (n>1) in line 2n, followed by the 4th number of 2n-1 lines, the 6th number of 2n-2 lines ... The sum of these numbers is the Fibonacci number of the first 4n-2. To arrange the number of rows, you can get 11n−1 11^{n-1} (n is the number of rows): 1 = 110 11^0 11 = 111 11^1; 121 = 112 11^2 ... When the n>5 does not conform to this one property, at this time should be the nth row of the most right number "1" in a single digit, and then the left of a number of single-digit to 10, and so on, the vacancy with "0" to fill, and then add all the numbers, the number is exactly 11n−1 11^{n-1}. In the case of n=11, the number of line 11th is: 1,10,45,120,210,252,210,120,45,10,1, the result is 25937424601=1110. 3. The algorithm principle of printing Yang Hui's triangle Diagram of the relationship between the line I element and the i+1 line element:
from row I of the data out of the team and calculate the i+1 row of data to the queue diagram:
4. Using the queue to achieve line-by-row printing Yang Hui's triangle of the first n rows Definition of node structure of 4.1 linked list

file: LinkNode.h

#ifndef link_node_h_


#define Link_node_h_



#include <iostream>


#include <string>


# Include <strstream>


using namespace std;

Template <class t>
struct linknode         //Link List node class definition
{
    T data;             Data domain
    linknode<t> *link;  Pointer field--subsequent pointer
    //only constructor that initializes pointer members
    Linknode (linknode<t>* ptr = NULL) {link = ptr;}
    Initializes the constructor of the data with the pointer member
    linknode (const t& value, linknode<t>* ptr = NULL) {data = value; link = ptr;}
};


#endif/* link_node_h_ * *
class definition of 4.2 chained queues and implementation of its Operation

file: LinkedQueue.h

#ifndef linked_queue_h_ #define Linked_queue_h_ #include "LinkNode.h" #include "Queue.h" template <class T&gt
;                      Class Linkedqueue:public queue<t> {Public:linkedqueue ();             Constructor virtual ~linkedqueue ();       The destructor public:virtual bool GetHead (t& x) const;       Reads the team head element and saves the value of the element to the X virtual bool EnQueue (const t& x);             New element x Team virtual bool Dequeue (t& x);           Team head element out of the team and save the value of the element to X virtual BOOL IsEmpty () const;            Determines whether the queue is empty virtual bool Isfull () const;               Determines whether the queue is full virtual void makeempty ();            Empty the contents of the queue virtual int getsize () const; Count the number of elements in a queue public:template <class t> friend ostream& operator<< (ostream& os, const linkedque   ue<t>& q); Overloaded operations of elements in output queues << private:linknode<t> *front;  Team head pointer, that is, the chain head pointer linknode<t> *rear;

Team tail pointer, that is, chain tail pointer}; Constructor template <class t> linkedqueue<t>::linkedqueue (): Front (null), rear (null) {cout << "$ execute constructor" << Endl;}
    destructor Template <class t> linkedqueue<t>::~linkedqueue () {cout << "$ perform destructor" << Endl;
Makeempty (); //Read the team head element and save the value of the element to the X template <class t> bool Linkedqueue<t>::gethead (t& x) const {if (true = = Isem
    Pty ()) {return false;
    } x = front->data;
return true;  }//new element x team template <class t> bool Linkedqueue<t>::enqueue (const t& x) {linknode<t> *newnode =
    New linknode<t> (x);
    if (NULL = = NewNode) {return false;
        } if (NULL = = Front) {front = NewNode;
    Rear = NewNode;
        else {rear->link = NewNode;
    Rear = rear->link;
return true; 
    //Team header element out of the team and save the value of the element to the X template <class t> bool linkedqueue<t>::D equeue (t& x) {if (true = = IsEmpty ())
    {return false; } LINKNODE&LT
    t> *curnode = front;
    Front = front->link;
    x = curnode->data;
    Delete Curnode;
return true; //Determine if the queue is empty template <class t> bool Linkedqueue<t>::isempty () const {return (NULL = = front)? true:fa
Lse

//Determine if the queue is full template <class t> bool Linkedqueue<t>::isfull () const {return false;}
    Emptying the contents of the queue template <class t> void Linkedqueue<t>::makeempty () {linknode<t> *curnode = NULL;            while (NULL!= front)//When the list is not empty, delete all nodes in the list {curnode = front;      Save deleted node Front = curnode->link;             The next node of the deleted node becomes the head node delete curnode;
    Remove the deleted node from the list}}//Calculate the number of elements in the queue template <class t> int linkedqueue<t>::getsize () const {int count = 0;
    linknode<t> *curnode = front;
        while (NULL!= curnode) {Curnode = curnode->link;
    count++;
return count; Overloaded operations of elements in//output queues << template <class t> ostream&operator<< (ostream& os, const linkedqueue<t>& q) {int i = 0;
    linknode<t> *curnode = Q.front; while (NULL!= curnode) {os << [<< i++ <<] "<<": "<< Curnode->data &
        lt;< Endl;
    Curnode = curnode->link;
} return OS;
 } #endif/* linked_queue_h_ * *
4.3 The realization of the first n rows of the Yang Hui's triangle by using the queue to realize line-by-row printing

file: YANGVI.h

#ifndef yangvi_h_ #define Yangvi_h_ #include "LinkedQueue.h" void set_space (int count, int i) {cout <<
    Endl
        while (I <= count + 1) {cout << "";
    i++;
    } template <class t> void Yangvi (linkedqueue<t>* linkedqueue, int n) {//branch print the factor of the two-item (A+B) ^n expansion.
    A queue is used in a program to put the coefficients of its next row into the queue, inserting a 0 between the row coefficients, when the line factor is output.
    cout << "=> print Yang Hui's triangle, row number n =" << n << Endl;
    int i = 1;
    int s = 0;
    int k = 0;
    int t = 0;
    Linkedqueue->enqueue (i);
    Linkedqueue->enqueue (i);
        for (i = 1; I <= n; i++) {Set_space (n, i);
        Linkedqueue->enqueue (k);
            for (int j = 1; J <= i + 2; j +) {Linkedqueue->dequeue (t);
            Linkedqueue->enqueue (s + t);
            s = t;
            if (j!= i + 2) {cout << s << "";
}} cout << Endl; } #endif/* yangvi_h_ * *
 
4.4 Implementation of main function (main function)

file: Main.cpp

#include "YANGVI.h"//to determine whether each character of the input string is numeric 0~9 bool Isnumber (const string& s_num) {for (size_t i = 0; i < S_nu M.size (); i++) {if (S_num[i] < ' 0 ') | |
        (S_num[i] > ' 9 ")
        {return false;
} return true;
    }//input line number n int get_n () {cout << > Input line number, n = ";
    String S_n;
    Cin >> S_n;
        while (false = = Isnumber (s_n)) {cout << "* Input is incorrect, please re-enter:";
    Cin >> S_n;
Return Atoi (S_n.c_str ()); }//Construct chain queue template <class t> linkedqueue<t>* construct_linkedqueue () {cout << "\n==> Create chained Queues" &
    lt;< Endl;
    linkedqueue<t> *linkedqueue = new linkedqueue<t>;
return linkedqueue; }//destructor chain queue template <class t> void Destory_linkedqueue (linkedqueue<t>* linkedqueue) {cout << "\n==
    > Release the space that the chain queue applies to in the heap and place the pointer variable pointing to the space as null "<< Endl;"
    Delete Linkedqueue;
Linkedqueue = NULL; int main (int argc, char* argv[]) {Linkedqueue<int> *linkedqueue = construct_linkedqueue<int> ();
    int n = get_n ();
    Yangvi (Linkedqueue, N);
    Destory_linkedqueue (Linkedqueue);
    System ("pause");
return 0; }
4.5 Yang Hui's triangle print results console output, when the Yang Hui's Triangle row number n=6.

Reference Documents:
[1] data structure (described in object-oriented method and C + + language) (2nd edition) Yin--Chapter III
[2] Baidu search keywords: Yang Hui's triangle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.