#39289: 不用低批題解(c++)


wheat20131@gmail.com (我有一天會被dp搞死)

學校 : 高雄市立三民高級中學
編號 : 218090
來源 : [114.47.81.49]
最後登入時間 :
2024-05-17 18:36:45
k740. 楊輝三角形 | From: [61.227.59.142] | 發表日期 : 2024-02-01 21:46

#include <bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int a;
    cin >> a;
    if(a == 1){
        cout << 1 << endl;
    }
    else if(a==2){
        cout << 1<< endl;
        cout <<"1 1" << endl;
    }
    else{
        int x[2] = {1,1};
        vector<int> v;
        v.push_back(x[0]);
        v.push_back(x[1]);
        cout << 1 << endl;
        cout << "1 1" << endl;
        for(int i = 3;i <=a;i++){
            int y[i];
            y[0] = 1;
            y[i-1]=1;
            for(int j = 1; j <= i-2;j++){
                y[j] = v[j-1]+v[j];
            }
            v.clear();
            for(int k = 0;k < i;k++){
                cout << y[k] <<" " ;
                v.push_back(y[k]);
            }  
            cout << endl;
           (未優化也僅為3ms,336KB)
           
           
           

 

        }
    }



    return 0;
}
 
 
ZeroJudge Forum