--- title: "[社課共筆]20250923-競程基礎帶您(差點)破台CPE" description: tags: - 社課 - 共筆 --- :::success # 競程基礎帶您(差點)破台CPE **時間:** 2025/09/23(星期二)18:30 ~ 21:00 **地點:** 挺生大樓 A3-200 教室 **簡報:** [連結](https://slides.com/speedcubin/deck-1819e2) **回饋表單:** [連結](https://forms.gle/bDqajXdu2LjKBYfK8) ::: > 從這裡開始 docs: https://hackmd.io/@speedcubing/competitive-programming 入門資料結構與演算法:https://www.hello-algo.com/zh-hant/ 比賽制度: - OI - ICPC ## I/O ```cpp #include <iostream> using namespace std; int main() { int N; int arr[100]; while(cin >> N) { // 一直讀,直到讀不到東西 for(int i = 0; i < N; i++) // 讀 N 個數字 cin >> arr[i]; /* do something */ } return 0; } ``` ```cpp #include <iostream> using namespace std; int main() { int N; int arr[100]; while(cin >> N) { // 一直讀,直到讀不到東西 string line; getline(cin, line); // 讀取整行 stringstream ss(line); // 將字串轉換成字串流(aka. 可以被讀取的東東) int num; while(ss >> num) { // 讀取一行內所有的數字 /* do something */ } } return 0; } ``` ## STL Container Standard Template Library(STL) 可以用來存放/管理元素 非常實用 ### Iterator 迭代器,跟指標很像 ```cpp= vector<int> v; v.begin(); // 取排頭 v.end(); // 取排尾的後一格 v.rbegin(); // 取排尾 v.rend(); // 取排頭的前一格 // 用星號取出內容 x = *v.begin() //取第一個元素的內容 ``` ### vector 可以動態改變大小的陣列 可以取代原版陣列 <!-- 我的GPT沒有 vector 插頭 --> <!-- 笑死 --> :::spoiler 挖空練習:12015 Google is Feeling Lucky ```c= #include <bits/stdc++.h> using namespace std; int main() { int T, score; cin >> T; // T 筆測資 string url; // 讀進來 url 的 buffer vector<string> output; // 輸出的 urls for (int t = 1; t <= T;t++) { cout << "Case #" << t << ":\n"; int max = -1; // 一筆 10 個 url for (int i = 0; i < 10; i++) { cin >> url >> score; if (score >= max) { // 如果目前的分數大於紀錄的最高分數 if (score > max) { output.clear(); // 清空vector } max = score; output.push_back(url); } } for(auto name : output) { cout << name <<"\n"; } } } ``` ::: :::spoiler Inception 全面啟動!!! ```cpp= #include <bits/stdc++.h> using namespace std; int main() { stack<string> dreams; int T; cin >> T; while (T--) { string s; cin >> s; if (s == "Sleep") { string name; cin >> name; dreams.push(name); } else if(s == "Kick") { if(!dreams.empty()) dreams.pop(); } else if(s == "Test") { if(dreams.empty()) cout << "Not in a dream\n"; else cout << dreams.top() <<"\n"; } } } ``` ::: :::spoiler Andy’s First Dictionary 詠竣的第一個字典 ```cpp= #include<bits/stdc++.h> using namespace std; int main() { set<string> words; string s; char c; while ((c = getchar()) != EOF) { if (isalpha(c)) { s += tolower(c); } else { words.insert(s); s = ""; } } words.erase(""); for(string w : words) { cout << w << "\n"; } } ``` ::: 社遊 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up