<style> .reveal .slides { text-align: left; font-size:32px; } </style> ## 530競技程式培力基地(一) ---- 先註冊[Virtual Judge](https://vjudge.net) ---- - 標頭檔 - 基本語法 - 四則運算 - **if else** - **for** and **while** loop --- ## 萬用標頭檔 `#include<bits/stdc++.h>` ---- 我知道大家都很懶,所以我給大家一個小模板:grin: ```cpp= #include<bits/stdc++.h> using namespace std; int main(){ return 0; } ``` --- ## 基本語法 ---- 我們來熟悉一下基本語法 程式設計應該都教過了 我這邊就快速帶過 ---- C++輸入輸出 ```cpp= int a; // 設定整數a cin >> a; // 假設輸入 13 cout << a; // 輸出 13 ``` ---- 多重輸入怎麼辦 ```cpp= int a,b,c; cin>>a>>b>>c; ``` ---- 說了這麼多你當然也可以用C語言的 ```cpp= scanf("%d",&a); printf("%d",a); ``` ---- data type ```cpp= int a;//是-2147483648 到 2147483647 char ch;//知道是表示字元就好 long long num; //-9223372036854775808 到 9223372036854775807 bool flag;// 0 或 1 double f; //1.7e-308 到 1.7e308 ``` 字元會比較難理解一點,我這邊附上[連結](https://hackmd.io/@Onelight/rkS3VdaE9) (這是別人的note) --- ## 四則運算 ---- ```cpp= int a=5,b=4,c; c = a + b;//c = 9 c = a - b;// c = 1 c = a * b;//c = 20 c = a / b;//c = 1 c = a % b;// c = 1 ``` --- ## if else ---- ```cpp= int a=10,b=5; if(a>b){ cout<<a<<endl; } else{ cout<<b<<endl; } ``` ---- 我們可以寫成這樣 ```cpp= int a=10,b=20,c=30; if(a>b && a>c){// && 是and的意思 cout<<a<<endl; } else if(b>a && b>c){ cout<<b<<endl; } else{ cout<<c<<endl; } ``` --- ## **for while** ---- ```cpp= for(int i=0 ; i<10 ; i++){ cout<<i<<' '; } ``` 上面的會輸出 0~9 總共十個數字 ---- ```cpp= int idx=0; while(idx<10){ cout<<idx<<' '; idx++; } ``` 上面的也是會輸出 0~9 總共十個數字 --- 實戰練習一下吧 [Triangle Wave](https://vjudge.net/problem/UVA-488) ---- 在這個問題中,你需要根據給定的一對振幅與頻率產生三角波形。 第一行輸入 $t$ 是測資數量 每個測資包含兩個正整數,分別位於兩行: 第一個整數為振幅 $A$ 第二個整數為頻率 $F$ 對於給定的振幅 $A$ 與頻率 $F$,你要印出 $F$ 個三角形(每個三角形由高度從 1 增至 $A$ 再降至 1 的整數列組成) ---- 那要怎麼寫呢? ---- 不管怎麼樣先把標頭和輸入打好 ```cpp= #include<bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; while(t--){ int A,F; cin>>A>>F; } return 0; } ``` ---- 接著處理三角形上半的部分 ```cpp= for(int i=1;i<=A;i++){ for(int j=0;j<i;j++){ cout<<i; } cout<<endl; } ``` 上面的輸出會是像這樣 1 22 333 ---- 接著處理三角形下半的部分 ```cpp= for(int i=A-1;i>0;i--){ for(int j=0;j<i;j++){ cout<<i; } cout<<endl; } ``` 上面的輸出會是像這樣 22 1 ---- 把頻率的部分放進去 最後把輸出修改一下 ```cpp= while(F--){ //三角形上半部分 for(int i=1;i<=A;i++){ for(int j=0;j<i;j++){ cout<<i; } cout<<endl; } //三角形下半部分 for(int i=A-1;i>0;i--){ for(int j=0;j<i;j++){ cout<<i; } cout<<endl; } if(F>=1)cout<<endl; } ``` ---- [答案放這](https://hackmd.io/gpgSjlxCSVyHZZZfW1D05g?view) --- 大家寫一下題目吧 [題單連結](https://vjudge.net/contest/748501#problem) 密碼 : 530CPEB
{"title":"530競技程式培力基地(一)","description":"530競技程式培力基地","contributors":"[{\"id\":\"2f04e9e4-ce0e-4fa7-9f7e-a3c9ae53239b\",\"add\":3479,\"del\":763,\"latestUpdatedAt\":1758516208936}]"}
    539 views