藍橋杯練習——2.22
文章目錄
- 藍橋杯練習——2.22
- 代碼練習
- 視頻學習
代碼練習
藍橋試題-基礎練習-楊輝三角形
問題描述
楊輝三角形又稱Pascal三角形,它的第i+1行是(a+b)i的展開式的系數,
它的一個重要性質是:三角形中的每個數字等于它兩肩上的數字相加,
下面給出了楊輝三角形的前4行:
1
1 1
1 2 1
1 3 3 1
給出n,輸出它的前n行,
輸入格式
輸入包含一個數n,
輸出格式
輸出楊輝三角形的前n行,每一行從這一行的第一個數開始依次輸出,中間使用一個空格分隔,請不要在前面輸出多余的空格,
樣例輸入
4
樣例輸出
1
1 1
1 2 1
1 3 3 1
資料規模與約定
1 <= n <= 34,
題解:
? 問題觀察:
? 觀察輸出樣例,除了第一行第一個為1的數字外,其他數字均為該數字位置的上一行同列與上一行上一列數字之和,所以可以建立一個二維陣列a[][]來處理,即
a
[
i
]
[
j
]
=
a
[
i
?
1
]
[
j
]
+
a
[
i
?
1
]
[
j
?
1
]
a[i][j] = a[i-1][j] + a[i-1][j-1]
a[i][j]=a[i?1][j]+a[i?1][j?1]
? 代碼分析:
? 用全域變數建立二維陣列,所有變數初始化為0,a[1][1]初始化為1,然后依照以上公式計算輸出即可,
? 代碼實作:
#include <bits/stdc++.h>
#define int long long
#define re register
#define il inline
using namespace std;
int n, a[40][40];
signed main()
{
ios::sync_with_stdio(false);
cin >> n;
a[1][1] = 1;
cout << 1 << endl;
for(int i = 2; i <= n; i++)
{
for(int j = 1; j <= i; j++)
{
a[i][j] = a[i-1][j]+a[i-1][j-1];
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
視頻學習
2019年藍橋杯訓練營(C++) 3.1-使用sort排序視頻講解
學習心得:有關sort函式用法
練習:LUOGU-P1156 垃圾陷阱
代碼:
#include <bits/stdc++.h>
#define int long long
#define re register
#define il inline
using namespace std;
struct node
{
int time, hgt, life;
} gunk[110];
int d, n;
int ti[110];
int dp[110]; //dp[height]=life
bool cmp(node a, node b)
{
return a.time < b.time;
}
signed main()
{
ios::sync_with_stdio(false);
cin >> d >> n;
for(re int i = 1; i <= n; ++i)
cin >> gunk[i].time >> gunk[i].life >> gunk[i].hgt;
sort(gunk + 1, gunk + n + 1, cmp);
dp[0] = 10;
for(re int i = 1; i <= n; ++i)
for(re int j = d; j >= 0; --j)
if(dp[j] >= gunk[i].time) //survive
{
if(j + gunk[i].hgt >= d) //climb out
{
cout << gunk[i].time;
return 0;
}
dp[j + gunk[i].hgt] = max(dp[j], dp[j + gunk[i].hgt]); //climb up
dp[j] += gunk[i].life; //continue life(?
}
cout << dp[0] << endl;
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/263363.html
標籤:其他
上一篇:Codeforces 1492D - Genius‘s Gambit (構造)
下一篇:海康工業相機引數設定與獲取
