"Yang Hui triangle (ID2032)" Problem solving report of Hangzhou Judge Online

Source: Internet
Author: User

Hangzhou Onlinejudge of " Yang Hui triangle ( ID2032)" Problem solving report

Qiao is clumsy (welcome reprint, but please specify Source: Http://blog.csdn.net/qiaoruozhuo )

Problem Description

Remember the Yang Hui triangle you learned in high school? Specific definitions are no longer described here, you can refer to the following graphs:

1

1 1

1 2 1

1 3 3 1

1 4 6) 4 1

1 5 10 10 5 1

Input

The input data contains multiple test instances, and each test instance input contains only one positive integer n (1<=n<=30), representing the number of layers of the Yang Hui triangle to be output.

Output

For each input, output the Yang Hui triangle of the corresponding number of layers, separated by a space between the integers in each layer, and a blank line behind each Yang Hui triangle.

Sample Input

2 3

Sample Output

1

1 1

1

1 1

1 2 1

Algorithm Analysis:  

the subject can be regarded as a simple application of dynamic programming algorithm. Depending on the complexity of the space, I wrote 4 different implementations.

algorithm 1 : Using the most primitive dynamic programming thinking, a two-dimensional array is used to record the elements of the Yang Hui Triangle. Starting from the first line, using the recursive relationship:a[i][j] =a[i-1][j-1] + a[i-1][j]; calculates the element value for the next line.

algorithm 2 : Observe the recurrence relationship and note that the first I the row element value is determined by the i-1 OK, so there is no need to record the element values for each row, just two lines is enough. We can record a row and the current output line element of Yang Hui triangle with two one-dimensional array, using recursion relation:Currow[j] =prerow[j-1] + prerow[j]; calculation.

Note that the output of each line element should be timely Currow[j] Copy the value of the Prerow[j] in order to iterate over the calculation.

algorithm 3 : Further observation of the recursive relationship, we found that only the previous line of the prerow[j-1] and Prerow[j] Two elements, so there is no need to record the entire line of elements, Just use two variables to iterate over the two element values.

Two variables can be set Left and the Right , stored separately Prerow[j-1] and the Prerow[j] value, and then take advantage of the recursive relationship: Currow[j] = left +right; Update Currow[j] values and update them in a timely manner Left and the Right the value.

algorithm4: Algorithm3Two variables are required becausePrerow[j-1]and thePrerow[j]values are updated, they need to be recorded before they can beCurrow[j]the value. If we go from right to left, then the updated element is onlyPrerow[j], whilePrerow[j]byCurrow[j]substitution, even if it is updated, does not affect derivationCurrow[j]the value. So we can calculate from right to left .Currow[j]value, so you don't have to use extra variables to recordPrerow[j-1]and thePrerow[j]the value.

Description:  

Algorithm idea: Dynamic programming.

Data structures: arrays, basic data types.

complexity of Time: algorithm 1O (n*n) , other algorithms O (n) .

12464653

2014-12-11 14:05:25

accepted

2032

0ms

1060k

P align= "center" >937 B

c

so clumsy

12464646

2014-12-11 14:04:39

Accepted

2032

15ms

1072K

983 B

c

clumsy

12464635

2014-12-11 14:03:37

accepted

2032

0ms

1064k

P align= "center" >1034 B

c

so clumsy

12464617

2014-12-11 14:02:10

Accepted

2032

0MS

1060K

831 B

C

If you are clumsy

The code is as follows:

#include <stdio.h> #define MaxRow 30void triangle_1 (int row),//use a two-dimensional array to record Yang hui element void triangle_2 (int row) of the triangle rows;// Use two one-dimensional arrays to record Yang Hui and output line elements void Triangle_3 (int row);//Use a one-dimensional array and two variable outputs Yang hui triangular void triangle_4 (int row);//    Use a one-dimensional array from right to left to record Yang Hui triangle int main () {int n;while (scanf ("%d", &n)! = EOF) {triangle_1 (n);p rintf ("\ n");} return 0;}    void triangle_1 (int row)//use two-dimensional array to record Yang Hui triangle line element {int a[maxrow+1][maxrow+1] = {1};        int I, J; for (I=1; i<=row; i++)//record each line element {for (j=1; j<=i; J + +) {A[i][j] = A[i-1][j-1] + a[i-1]            [j];            printf ("%d", a[i][j]);        if (J < i) printf ("");    } printf ("\ n"); }}void triangle_2 (int row)//use two one-dimensional array to record Yang Hui on a row and output line element {int prerow[maxrow+1] = {0};//stores a row of elements int currow[maxrow+1] =        {0};//stores the output line element int I, J; PREROW[1] = 1; Initialize data for (i=1; i<=row; i++)//record each line element {for (j=1; j<=i; J + +) {Currow[j] = PreRow         [j-1] + prerow[j]; } for (J= 1; j<=i;            J + +) {Prerow[j] = currow[j];            printf ("%d", currow[j]);        if (J < i) printf ("");    } printf ("\ n");        }}void triangle_3 (int row)//use a one-dimensional array and two variable outputs Yang hui triangle {int currow[maxrow+1] = {0};//store output line element int I, J, left, right; CURROW[1] = 1;            Initialize data for (i=1; i<=row; i++) {left = 0;//Initialize data for (j=1; j<=i; j + +)//record output line element {             right = Currow[j];             CURROW[J] = left + right;            printf ("%d", currow[j]);            if (J < i) printf ("");        left = right;    } printf ("\ n");        }}void triangle_4 (int row)//use a one-dimensional array from right to left to record Yang Hui triangle {int currow[maxrow+1] = {0};//store output line element int I, J; CURROW[1] = 1;  Initialize data for (i=1; i<=row; i++) {for (j=i; j>=1; j--)//right-to-left record output line element {Currow[j] + =         CURROW[J-1];        } for (j=1; j<=i; J + +) {printf ("%d", currow[j]);    if (J < i) printf ("");    } printf ("\ n"); } }


"Yang Hui triangle (ID2032)" Problem solving report of Hangzhou Judge Online

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.