| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 10829 | Pascal triangle Nth number (function) |
|
Description
Pascal's triangle is a triangular array of the binomial coefficients. You need to print the element k on row of Pascal's triangle, which can be calculated by the following :
C(n, 0) = 1, for n = 1, 2, …
C(n, n) = 1, for n = 1, 2, …
C(n, k) = C(n-1, k-1) + C(n-1, k), for k = 2, 3, … and for n = 2, 3, …
Therefore, the first 6 rows would be
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
And for example:
If n_row=3 and k_element=4, you should print 1, because the row 3 of Pascal triangle is "1 3 3 1", and 4th number is 1.
And if n_row=6 and k_element=3, you should print 15, because the row 6 of Pascal triangle is "1 6 15 20 15 6 1", and 3rd number is 15.
Note: The input n_row range is 1~25, input k_element range is 1~26.
Note that
1. This problem involves three files.
- function.h: Function definition of pascalNthNum.
- function.cpp: Function describe of pascalNthNum.
- main.cpp: A driver program to test your implementation.
You will be provided with main.cpp and function.h, and asked to implement function.cpp.
2. For OJ submission:
Step 1. Submit only your function.cpp into the submission block. (Please choose c++ compiler)
Step 2. Check the results and debug your program if necessary.
function.h
void pascalNthNum(int n_row, int k_element);
main.cpp
#include <stdio.h>
#include "function.h"
int main()
{
int row = 0;
int kth = 0;
while(scanf("%d", &row) && row != 0){
scanf("%d", &kth);
pascalNthNum(row, kth);
}
return 0;
}
Input
There are two positive integers Row (1<=Row<=25) and kth element(1 <= kth <= (Row+1)) in each line, if input Row is 0 then stop the program.
Output
Print the the kth element in the nth row.
And there is a ‘\n’ at the end of each line except input Row is 0.