20級爪哇程式設計新生賽題解
- 20級爪哇程式設計新生賽1.0(正式賽)
- A.The Tree Of LittleZhua(思維或者線段樹)
- Easy Version
- Hard Version
- B.小爪的破譯
- C.小爪的博弈(巴什博弈)
- D.小爪的乒乓球比賽(暴力或者數學計算)
- E.小爪牌消消樂(區間DP)
- F.小爪做安卓組考核(堆疊)
- G.小爪的統計資料
- H.小爪跳臺階
- I.小爪的除數
- J.小爪的數學題(前綴和)
- K.小爪的試煉
- L.小爪的榮耀(模擬)
- 20級爪哇程式設計新生賽1.0(熱身賽)
- A.小爪學編程(水題)(熱身賽)
- B.小爪的座駕(熱身賽)
- C.小爪的AC(最長上升子序列)(熱身賽)
- D.小爪找規律(熱身賽)
20級爪哇程式設計新生賽1.0(正式賽)
A.The Tree Of LittleZhua(思維或者線段樹)
Easy Version
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INT_MAX 0x3f3f3f3f
#define INT_MIN 0xc0c0c0c0
using namespace std;
const int N = 200000;
int arr[N];
signed main()
{
int n, m;
scanf("%lld%lld", &n, &m);
for(int i = 0; i < n; i++) scanf("%lld", &arr[i]);
for(int i = 0; i < m; i++) {
char ch;
int a, b;
getchar();
scanf("%c%lld%lld", &ch, &a, &b);
if (ch == 'Q') {
int maxx = 0;
for (int j = a - 1; j < b; j++) {
if(arr[j] > maxx) maxx = arr[j];
}
printf("%lld\n", maxx);
} else arr[a - 1] = b;
}
return 0;
}
Hard Version
線段樹相關知識點原理及常見題型 Click here~~
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
//#define int ll
#define INF 0x3f3f3f3f
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); }
return s * w;
}
const int N = 200010;
int m, p;//m操作次數,p取模的值是多少
struct Node{
int l, r;//左端點,右端點
int val;//區間[l, r]的最大值
}tr[N << 2];
int num[N];
//由子節點的資訊來計算父節點的資訊
void pushup(int cur){
tr[cur].val = max(tr[cur << 1].val, tr[cur << 1 | 1].val);
}
//cur代表當前節點,
void build(int cur, int l, int r){
//當前結點的左右兒子分別是tr[cur].l tr[cur].r
tr[cur] = {l, r};
//如果已經是葉結點return
if(l == r) {
tr[cur].val = num[r];
return;
}
//否則求一下當前區間的中點
int mid = l + r >> 1;
//遞回建立左邊區間
build(cur << 1, l, mid);
//遞回建立右邊區間
build(cur << 1 | 1, mid + 1, r);
pushup(cur);
}
//[l, r]查詢區間 cur代表當前線段樹里面的端點,
int query(int cur, int l, int r) {
//①情況[TL,TR] ? [L,R]
//樹中節點,已經被完全包含在[l, r]中了,
if (tr[cur].l >= l && tr[cur].r <= r) {
return tr[cur].val;
}
int mid = tr[cur].l + tr[cur].r >> 1;
int val = 0;
//判斷與左邊有沒有交集
if (l <= mid) {
val = query(cur << 1, l, r);
}
//這里為什么是 r > mid,因為劃分的區間是[l, mid][mid + 1, r],所以要用>而不能=
//判斷與右邊有沒有交集
if (r > mid) {
//為什么要取max?
//因為上面先比較左值,所以左值可能有一個最大值,要跟再右邊區間查詢的值進行比較,取最大的,
val = max(val, query(cur << 1 | 1, l, r));
}
//回傳結果
return val;
}
//cur代表當前線段樹里面的端點,tar代表要修改的位置
void modify(int cur, int tar, int val) {
//如果當前節點就是葉節點,那么直接修改就可以了
if (tr[cur].l == tar && tr[cur].r == tar) {
tr[cur].val = val;
return;
}
int mid = tr[cur].l + tr[cur].r >> 1;
if (tar <= mid) {
modify (cur << 1, tar, val);
} else {
modify (cur << 1 | 1, tar, val);
}
//遞回完之后,要更新到父節點,
//pushup就是更新父節點的資訊
pushup(cur);
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)){
memset(num, 0, sizeof num);
memset(tr, 0, sizeof tr);
for (int i = 1;i <= n; ++i) {
scanf("%d", &num[i]);
}
build(1, 1, n);
for (int i = 0; i < m; ++i) {
char ch;
int a, b;
getchar();//每一次都要getchar();放在回圈里面的第一行,
scanf("%c%d%d",&ch,&a,&b);
if (ch == 'Q') {
printf("%d\n", query(1, a, b));
}
else{
modify(1, a, b);
}
}
}
return 0;
}
B.小爪的破譯
字串b:
a b b a c c
b[1] b[2] b[3] b[4] b[5] b[6]
字串a:
a b a c
a[1] a[2] a[3] a[4]
已知字串b即已知字串b的長度
通過觀察得出規律:
兩字串之間的長度規律:b.length() = 2 × (a.length() - 1)
兩字串之間的數值規律:a[i] = b[2 × i - 1]
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
getchar();
while (t--) {
char b[105];
scanf("%s", b);
for (int i = 0; i < strlen(b) - 1; i += 2) {
printf("%c", b[i]);
}
printf("%c\n", b[strlen(b) - 1]);
}
return 0;
}
C.小爪的博弈(巴什博弈)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INT_MAX 0x3f3f3f3f
#define INT_MIN 0xc0c0c0c0
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); }
return s * w;
}
signed main()
{
int t = read();
while (t--) {
int n = read();
int m = read();
if (n % (m + 1)) printf("first\n");
else printf("second\n");
}
return 0;
}
D.小爪的乒乓球比賽(暴力或者數學計算)
暴力解法:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<map>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
signed main()
{
int t = read();
while (t--) {
int n = read();
int cnt = 0;
while (n > 1) {
if (n % 2 == 0) {
n /= 2;
cnt += n;
} else {
n /= 2;
cnt += n;
n++;
}
}
printf("%lld\n", cnt);
}
return 0;
}
數學解法:輸出n-1就可以了
原理決議:
n支隊伍參加淘汰賽決出獲勝隊伍需要場次是:n - 1
淘汰賽制:
那么有n支隊伍,要決出獲勝隊伍,那么意思就是要淘汰 n - 1支隊伍,一場比賽淘汰一支隊伍,則需要n - 1場比賽,
如果需要爭季軍:就是需要n場比賽(加多一場季軍賽)
#include<cstdio>
#include<iostream>
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
printf("%d\n", n - 1);
}
return 0;
}
E.小爪牌消消樂(區間DP)
區間DP的原理及例題 Click here ~
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
const int N = 410;//因為要開雙鏈
const int inf = 0x3f3f3f3f;
int sum[N], val[N];//sum[N]表示合并付出的代價總和,val[N]表示每個點的值,
int dpmin[N][N], dpmax[N][N];//得分總和最小,得分總和最大
int main()
{
int n = read();
for (int i = 1; i <= n; ++i) {
val[i] = read();
val[i + n] = val[i];//化環形為鏈形
}
for (int i = 1; i <= n + n; ++i) {
sum[i] = sum[i - 1] + val[i];//前綴和 以便最后求出——最后一步所需要付出的代價
}
//初始化
memset(dpmin, 0x3f, sizeof dpmin);
memset(dpmax, -0x3f, sizeof dpmax);
//列舉鏈的長度
for (int len = 1; len <= n; ++len) {
//列舉區間左端點
for (int l = 1; l + len - 1 <= n + n; ++l) {
int r = l + len - 1;//區間右端點
//如果區間長度為1,則無需付出任何代價,
if (len == 1) dpmin[l][r] = dpmax[l][r] = 0;
else {
//列舉分界線
for (int k = l; k < r; ++k) {
dpmin[l][r] = min(dpmin[l][r], dpmin[l][k] + dpmin[k + 1][r] + sum[r] - sum[l - 1]);
dpmax[l][r] = max(dpmax[l][r], dpmax[l][k] + dpmax[k + 1][r] + sum[r] - sum[l - 1]);
}
}
}
}
//初始化最大值最小值,
int maxv = -inf, minv = inf;
for (int l = 1; l <= n; ++l) {
maxv = max(maxv, dpmax[l][l + n - 1]);
minv = min(minv, dpmin[l][l + n - 1]);
}
//得出結果
printf("%d\n", minv);
printf("%d\n", maxv);
return 0;
}
F.小爪做安卓組考核(堆疊)
對數字運算進行模擬:
① 如果遇到 ’ + ’ 或 ’ - ',先將相應的數字push入堆疊(如果是 ’ - ’ 則push數字的相反數)
② 如果遇到優先級大的 ’ * ’ 或 ’ / ’ 先將相應數字與堆疊頂元素進行運算后將結果push進入
③ 最后將堆疊中所有元素相加,
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
signed main()
{
char c, s;
char s;
double n;
//開頭特殊處理:0 +'\n'時結束;
while(~scanf("%lf%c",&n,&c)) {
if(n == 0 && c=='\n') break;
stack<double>stk;
double ans=0;
while(!stk.empty()) stk.pop();
stk.push(n);
while(~scanf("%c %lf",&s,&n)) {
if (s == '+') stk.push(n);
if (s == '-') stk.push(-n);
if (s == '*') {
double tmp = stk.top() * n;
stk.pop();
stk.push(tmp);
}
if (s=='/') {
double tmp = stk.top() * 1.0 / n;
stk.pop();
stk.push(tmp);
}
c = getchar();
if (c == '\n') break;
}
while (!stk.empty()) {
ans += stk.top();
st.pop();
}
printf("%.2lf\n",ans);
}
return 0;
}
G.小爪的統計資料
題解:用一個陣列a[ ]進行計數,首先初始化a[ ]陣列,全部為0
比如說:3 2 3 4 3 2 1, 算出其中最大的數字 maxx = max(maxx, a[i]); max()函式是比較兩個數字大小;
如果是C語言的話需要自己寫一個max()函式
int max (int n) {
if (a >= b) return a;
else return b;
}
遍歷輸入的數字然后依次執行
a[3]++;
a[2]++;
a[3]++;
a[4]++;
a[3]++;
a[2]++;
a[1]++;
此時最大的數字為4
然后從最大往最小遍歷
for (int i = maxx; i >= 1; --i)
如果a[i] 不為 0,則輸出 if (a[i])
注意輸出格式,即可
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<map>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
const int N = 10010;
int a[N];
signed main() {
int n = read();
int maxx = 0;
memset(a, 0, sizeof a);
for (int i = 1; i <= n; ++i) {
int x = read();
a[x]++;
maxx = max(maxx, x);
}
for (int i = maxx; i >= 1; --i) {
if (a[i]) printf("%lld-%lld\n", i, a[i]);
}
return 0;
}
H.小爪跳臺階
題解:遞推
f
(
n
)
=
f
(
n
?
1
)
+
f
(
n
?
2
)
f(n)=f(n-1)+f(n?2)
f(n)=f(n?1)+f(n?2) 記得最后要 %1000000007
#include<iostream>
#include<cstdio>
const int N = 110;
int f[N];
int main()
{
int n;
scanf ("%d", &n);
if (n == 1) printf("1\n");
f[1] = 1;
f[2] = 2;
for (int i = 3; i <= n; i++) {
f[i] = (f[i - 2] + f[i - 1]) % 1000000007;
}
printf("%d\n", f[n]);
}
I.小爪的除數
題解:要求最少次數,所以自然是先 % 的數字越大,運算元最少
所以依次從 5 到 3 再到 2;
注意資料范圍很大要用 long long 定義變數
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); }
return s * w;
}
signed main()
{
int n = read();
while (n--) {
int x = read();
int ans = 0;
while (x > 1) {
int flag = 0;
if (x % 5 == 0) {
x = x / 5 * 4;
flag = 1;
} else if(x % 3==0) {
x= x / 3 * 2;
flag = 1;
} else if (x % 2 == 0) {
x = x / 2;
flag = 1;
}
ans++;
if (!flag) break;
}
if (x == 1) printf("%lld\n",ans);
else printf("-1\n");
}
return 0;
}
J.小爪的數學題(前綴和)
前綴和演算法是一個必須要掌握的演算法,詳細解釋Click here ~~
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<map>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
const int N = 1010;
int a[N];
int sum[N];
signed main() {
int n = read();
for (int i = 1; i <= n; ++i) {
a[i] = read();
sum[i] = sum[i - 1] + a[i];
}
int t = read();
while(t--) {
int l = read(), r = read();
if (l > n) {
printf("0\n");
continue;
}
if (r > n) r = n;
printf("%lld\n", sum[r] - sum[l - 1]);
}
return 0;
}
K.小爪的試煉
最長非回文字串包括三種情況:
① 如果這整個字串每個字符都一樣,那么此時最長的非回文子串長度 == 0
② 如果這整個字串是回文字串,那么此時最長的非回文子串長度 == s.size()-1
③ 這整個字串不是回文字串,那么此時最長非回文子串長度 == s.size()
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); }
return s * w;
}
//------------------------ 以上是我常用模板與刷題幾乎無關 ------------------------//
const int N = 50010;
string s;
signed main()
{
cin >> s;
int l = 0, r = s.size() - 1;
char a = s[0];
int flag = 1;
while (l <= r) {
if (s[l] != a || s[r] != a) flag = 0;
if (s[l] == s[r]) l++, r--;
else break;
}
if (l > r && flag) printf("0\n");//情況①
else if (l > r) printf("%lld\n", s.size() - 1);//情況②
else printf("%lld\n", s.size());//情況③
return 0;
}
L.小爪的榮耀(模擬)
注意一下空格的輸出方式
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<map>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
//最大公約數
int gcd(int x,int y) {
if(x<y) swap(x,y);//很多人會遺忘,大數在前小數在后
//遞回終止條件千萬不要漏了,輾轉相除法
return x % y ? gcd(y, x % y) : y;
}
//計算x和y的最小公倍數
int lcm(int x,int y) {
return x * y / gcd(x, y);//使用公式
}
int ksm(int a, int b, int mod) { int s = 1; while(b) {if(b&1) s=s*a%mod;a=a*a%mod;b>>=1;}return s;}
//------------------------ 以上是我常用模板與刷題幾乎無關 ------------------------//
signed main() {
int n = read();
int kase = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n + n; ++j) {
if (i == j) printf("V");
else if (j == n + n - kase) {
printf("V");
kase++;
}else printf(" ");
}
printf("\n");
}
return 0;
}
20級爪哇程式設計新生賽1.0(熱身賽)
A.小爪學編程(水題)(熱身賽)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<map>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
const int N = 200010;
signed main() {
int h1 = read(), m1 = read(), h2 = read(), m2 = read();
int h3 = 0, m3 = 0;
h3 = h2 - h1;
if (m1 > m2) {
h3--;
m3 = 60 - m1 + m2;
} else {
m3 = m2 - m1;
}
printf("%lld %lld\n", h3, m3);
return 0;
}
B.小爪的座駕(熱身賽)
題解:簡單的去重和排序
C++做法的STL sort()和unique()這兩個函式,就是這么簡單
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
scanf("%lld", &n);
int a[1010];
for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
sort(a, a + n);
int len = unique(a, a + n) - a;//去重,并回傳去重后的長度
printf("%lld\n", len);//輸出去重后的長度
for (int i = 0; i < len; i++) printf("%lld ",a[i]);
printf("\n");
return 0;
}
C語言做法:就要自己手寫兩個函式,第一個是排序,第二個去重,
#include<stdio.h>
int main()
{
int n;
int a[1010];
int b[1010];
int i, j, t;
int cnt;
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
//排序 可以用C++ 的STL sort()函式代替
for (i = 1; i <= n; i++) {
for (j = i + 1; j <= n; j++) {
if (a[i] > a[j]) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
//去重 可以用C++ 的STL unique()函式代替
cnt = 0;
for (i = 1; i <= n; i++) {
if (a[i] != a[i + 1]) {
cnt++;
b[cnt] = a[i];
}
}
printf("%d\n", cnt);
for (i = 1; i <= cnt; i++) printf("%d ",b[i]);
return 0;
}
C.小爪的AC(最長上升子序列)(熱身賽)
遍歷
如果遞增就cnt++,同時存一個最大值maxx
如果下一個cnt比這個最大值maxx大,那么就替換
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<map>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
signed main(){
int n = read();
int ans = 1, cnt = 1;
int pre = read();
n--;
while (n--) {
int cur = read();
if (pre < cur) cnt++;
else cnt = 1;
ans = max(ans, cnt);
pre = cur;
}
printf("%lld\n", ans);
return 0;
}
D.小爪找規律(熱身賽)
題解:
楊輝三角,有個小坑,注意一下每一行最后不能有空格
a
[
i
]
[
j
]
=
a
[
i
?
1
]
[
j
?
1
]
+
a
[
i
?
1
]
[
j
]
a[i][j]=a[i-1][j-1]+a[i-1][j]
a[i][j]=a[i?1][j?1]+a[i?1][j]
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<map>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
int a[25][25];
signed main()
{
int n = read();
for (int i = 1; i <= n; i++) a[i][1] = a[i][i] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 2; j < i; j++) {
a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
}
}
for (int i = 1; i <= n; i++) {
printf("%lld", a[i][1]);
for (int j = 2; j <= i; j++) {
printf(" %lld", a[i][j]);
}
printf("\n");
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/251814.html
標籤:其他
