当前位置:网站首页>楊輝三角形詳解
楊輝三角形詳解
2022-06-13 06:14:00 【qw&jy】
楊輝三角形
前提:每行端點與結尾的數為1。
每個數等於它上方兩數之和。
每行數字左右對稱,由 1 開始逐漸變大。
第 n 行的數字有 n 項。
前 n 行共[(1 + n) * n] / 2 個數。
第 n 行的 m 個數可錶示為 C(n - 1,m - 1),即為從 n - 1 個不同元素中取 m - 1 個元素的組合數。
第 n 行的第 m 個數和第 n - m + 1個數相等 ,為組合數性質之一。
每個數字等於上一行的左右兩個數字之和。可用此性質寫出整個楊輝三角。即第 n + 1 行的第 i 個數等 8 於第 n 行的第 i - 1 個數和第 i 個數之和,這也是組合數的性質之一。即 C(n + 1, i) = C(n, i) + C(n, i - 1)。
(a + b) * n的展開式中的各項系數依次對應楊輝三角的第 (n + 1) 行中的每一項。
將第 2n + 1 行第 1 個數,跟第 2n + 2 行第 3 個數、第 2n + 3 行第 5 個數……連成一線,這些數的和是第 4n + 1 個斐波那契數;將第 2n 行第 2 個數(n > 1),跟第 2n - 1 行第 4 個數、第 2n - 2 行第 6 個數……這些數之和是第 4n - 2 個斐波那契數。
將第 n 行的數字分別乘以10(m-1),其中m為該數所在的列,再將各項相加的和為11(n-1)。110 = 1,111 = 1 x 100 + 1 × 101 = 11,112 = 1 × 100 + 2 x 101 + 1 x 102 = 121,113 = 1 x 100 + 3 × 101 + 3 x 102 + 1 x 103 = 1331,114 = 1 x 100 + 4 x 101 + 6 x 102 + 4 x 103 + 1 x 104 = 14641,115 = 1 x 100 + 5 x 101 + 10 x 102 + 10 x 103 + 5 x 104 + 1 × 105 = 161051。
第 n 行數字的和為2(n-1)。1 = 2(1-1),1 + 1 = 2(2-1),1 + 2 + 1 = 2(3-1),1 + 3 + 3 + 1 = 2(4-1),1 + 4 + 6 + 4 + 1 = 2(5-1),1 + 5 + 10 + 10 + 5 + 1 = 2(6-1)。
斜線上數字的和等於其向左(從左上方到右下方的斜線)或向右拐彎(從右上方到左下方的斜線),拐角上的數字。1 + 1 = 2,1 + 1 + 1 = 3,1 + 1 + 1 + 1 = 4,1 + 2 = 3,1 + 2 + 3 = 6,1 + 2 + 3 + 4 = 10,1 + 3 = 4,1 + 3 + 6 = 10,1 + 4 = 5。
將各行數字左對齊,其右上到左下對角線數字的和等於斐波那契數列的數字。1,1,1 + 1 = 2,2 + 1 = 3,1 + 3 + 1 = 5,3 + 4 + 1 = 8,1 + 6 + 5 + 1 = 13,4 + 10 + 6 + 1 = 21,1 + 10 + 15 + 7 + 1 = 34,5 + 20 + 21 + 8 + 1 = 55。
例題
第一題
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n;
int main(){
scanf("%d", &n);
int num[50][50];
num[1][1] = 1;
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= i; j ++){
if(j == i || j == 1){
num[i][j] = 1;
printf("%d ", num[i][j]);
}
else{
num[i][j] = num[i - 1][j - 1] + num[i - 1][j];
printf("%d ", num[i][j]);
}
}
printf("\n");
}
return 0;
}
第二題
楊輝三角左右對稱(C(a, b) == C(a, a-b)),又由於找第一次出現,因此一定在左邊,右邊可以直接删掉!
性質:
- 每一斜行從上到下遞增
- 每一橫行從中間到兩邊依次遞减
倒序(從16開始)使用二分查找。
二分端點:l:2k r:max(n, l) n 為查找的數
右端點一定不能比左端點小!
特例:否則當 n = 1 時,會出問題。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int n;
//C(a, b) = a!/b!(a-b)! = a * (a-1) .. b個 / b!
LL C(int a, int b){
//求第a行第b列的值
LL cnt = 1;
for(int i = a, j = 1; j <= b; i --, j ++){
cnt = cnt * i / j;
if(cnt > n)//大於n沒有意義,放在超long long
return cnt;
}
return cnt;
}
bool check(int k){
// 二分該斜行,找到大於等於該值的第一個數
int l = 2 * k, r = max(n, l);
while(l < r){
int mid = l + r >> 1;
if(C(mid, k) >= n)
r = mid;
else
l = mid + 1;
}
if(C(r, k) != n)
return false;
printf("%lld", 1ll * (r + 1) * r / 2 + k + 1);//C(r, k)對應的順序值為:(r + 1) * r / 2 + k + 1
return true;
}
int main(){
scanf("%d", &n);
for(int k = 16; ; k --)
if(check(k))
break;
return 0;
}
第三題
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> num;
for(int i = 0; i < numRows; i ++){
vector<int> a(i + 1);
for(int j = 0; j <= i; j ++){
if(j == 0 || i == j)
a[j] = 1;
else
a[j] = num[i - 1][j - 1] + num[i - 1][j];
}
num.push_back(a);
}
return num;
}
};
第四題
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<vector<int>> num;
for(int i = 0; i <= rowIndex; i ++){
vector<int> a(i + 1);
for(int j = 0; j <= i; j ++){
if(j == 0 || i == j)
a[j] = 1;
else
a[j] = num[i - 1][j - 1] + num[i - 1][j];
}
num.push_back(a);
}
return num[rowIndex];
}
};
边栏推荐
- Leetcode- find all missing numbers in the array - simple
- Leetcode- hex number - simple
- Wechat applet (pull-down refresh data) novice to
- Use of Nacos configuration center
- The SQL file of mysql8.0 was imported into version 5.5. There was a pit
- How to view APK version number from apk
- js将文本转成语言播放
- USB status error and its cause (error code)
- Experience of redis installation under Linux system (an error is reported at the same time. The struct redis server does not have a member named XXXX)
- USB 0xc0000011 error
猜你喜欢
Test logiciel - résumé des FAQ d'interface
Echart矩形树图:简单实现矩形树图
华为开发者认证与DevEco Studio编译器下载
[DP 01 backpack]
Solutions to common problems in small program development
Use of Nacos configuration center
The boys x pubgmobile linkage is coming! Check out the latest game posters
A glimpse of [wechat applet]
Learning records countless questions (JS)
js将文本转成语言播放
随机推荐
Use of Nacos configuration center
Record the basic use of zxing, the Google open source library, to generate and read QR codes
Leetcode Timo attack - simple
Wechat applet: basic review
Local file search tool everything
[to]12 common IP commands in the iproute installation package
Echart柱状图:echart实现堆叠柱状图
[compilation and assembly link] coff file and structure description
Wechat applet (function transfer parameters, transfer multiple parameters, page Jump)
Free screen recording software captura download and installation
Echart柱状图:堆叠柱状图value格式化显示
Essays on November 5, 2021
Leetcode- reverse string ii- simple
Test logiciel - résumé des FAQ d'interface
高迸发解决方案2
CORS request principle
MySQL stored procedure
【美团笔试题】
Uni app provincial and urban linkage
Leetcode- minimum number of operations to make array elements equal - simple