程序填空题:Pascal Triangle
In mathematics, Pascal's triangle is a triangular array of the binomial coefficients that arise in probability theory, combinatorics, and algebra. In much of the Western world, it is named after the French mathematician Blaise Pascal, although other mathematicians studied it centuries before him in China. It is called Yanghui's triangle in Chinese.
The rows of Pascal's triangle are conventionally enumerated starting with row n=0 at the top (the 0th row). The entries in each row are numbered from the left beginning with k=0 and are usually staggered relative to the numbers in the adjacent rows. The triangle may be constructed in the following manner: In row 0 (the topmost row), there is a unique nonzero entry 1. Each entry of each subsequent row is constructed by adding the number above and to the left with the number above and to the right, treating blank entries as 0. For example, the initial number in the first (or any other) row is 1 (the sum of 0 and 1), whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row. So the first few lines of this triangle is like:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
...
The following function is used to get the *nth* line of Yanghui's triangle.
c
#include <stdio.h>
void yanghui(int line[], int n)
{
if (n == 1)
{
= 1;
}
else
{
;
int newline[n];
for (int i = 1; i < n - 1; i++)
{
newline[i] = ;
}
for (int i = 1; i < n - 1; i++)
{
line[i] = newline[i];
}
= 1;
}
}
const int LEN = 6;
int main()
{
int line[LEN];
yanghui(line, LEN);
for (int i = 0; i < LEN; i++)
{
printf("%2d ", line[i]);
}
printf("\n");
}
答案:
第1空: line[0]
第2空:yanghui(line, n - 1)
第3空:line[i - 1] + line[i]
第4空:line[n - 1]
The rows of Pascal's triangle are conventionally enumerated starting with row n=0 at the top (the 0th row). The entries in each row are numbered from the left beginning with k=0 and are usually staggered relative to the numbers in the adjacent rows. The triangle may be constructed in the following manner: In row 0 (the topmost row), there is a unique nonzero entry 1. Each entry of each subsequent row is constructed by adding the number above and to the left with the number above and to the right, treating blank entries as 0. For example, the initial number in the first (or any other) row is 1 (the sum of 0 and 1), whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row. So the first few lines of this triangle is like:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
...
The following function is used to get the *nth* line of Yanghui's triangle.
c
#include <stdio.h>
void yanghui(int line[], int n)
{
if (n == 1)
{
= 1;
}
else
{
;
int newline[n];
for (int i = 1; i < n - 1; i++)
{
newline[i] = ;
}
for (int i = 1; i < n - 1; i++)
{
line[i] = newline[i];
}
= 1;
}
}
const int LEN = 6;
int main()
{
int line[LEN];
yanghui(line, LEN);
for (int i = 0; i < LEN; i++)
{
printf("%2d ", line[i]);
}
printf("\n");
}
答案:
第1空: line[0]
第2空:yanghui(line, n - 1)
第3空:line[i - 1] + line[i]
第4空:line[n - 1]