2023/12/06
主講:ShiYu
下次社課為「期末練習賽👨🏻💻」+「聚餐🍕」
這次社課學完記得要回去複習過一遍之前教過的所有內容
資源都幫大家彙整在這了 > NFIRC 資源彙整
多多寫題單 期末賽的題目只要你有寫題單就會了
當條件成立時 重複執行直到條件不成立
while(條件成立時) { 重複執行這段程式碼 }
條件不成立時 電腦會退出迴圈並接著執行下面的程式
#include <bits/stdc++.h> using namespace std; int main() { int t = 100; while(t--) { cout << "hello\n"; } return 0; }
輸入
5
10 40 20 80 50
程式碼
#include <bits/stdc++.h> using namespace std; int main() { int n, a, sum = 0; cin >> n; while(n--) { cin >> a; sum += a; } cout << sum << "\n"; return 0; }
輸入
10 40 20 80 50
程式碼
#include <bits/stdc++.h> using namespace std; int main() { int n, sum = 0; while(cin >> n) { sum += n; } cout << sum << "\n"; return 0; }
重複執行特定次數
for(初始值; 判斷式; 改變值) {
重複執行這段程式碼
}
#include <bits/stdc++.h> using namespace std; int main() { for(int i = 1; i <= 10; i++) { cout << i << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { for(int i = 10; i > 0; i--) { cout << i << " "; } return 0; }
如果 n == 3 請輸出
1 2 4 5 6 7 8 9 10
如果 n == 8 請輸出
1 2 3 4 5 6 7 9 10
思考一下該怎麼做呢?
請輸出 1 ~ 10 但不要輸出 n
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for(int i = 1; i <= 10; i++) { if(i == n) { continue; } cout << i << " "; } return 0; }
相同類型的元素所組成的資料結構
佔用連續區段的記憶體
如果我們要儲存三個數字
可以使用三個變數來存
int a,b,c; cin >> a >> b >> c;
那如果我們要儲存 100 個數字呢?
用 100 個變數來存?
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z; cin >> a >> b >> c >> d >> e >> f >> g >> h >> i >> j >> k >> l >> m >> n >> o >> p >> q >> r >> s >> t >> u >> v >> w >> x >> y >> z;
顯然英文字母都不夠用了 而且超麻煩
與宣告變數的方法相同 只是多了 陣列大小
int a; // 宣告變數
int arr[5]; // 宣告陣列
示意圖
在 C++ 中 陣列 Array 的大小須事先宣告好
且為固定大小
若不知道要設多大
請仔細觀察題目給的數量範圍來決定
例如:有 \(n\) 個數字 \(1 \le n \le 1000\)
就可以把陣列大小開到 arr[1000] 以上
可以使用 C++ STL 內建資料結構中的 vector 動態陣列
下學期社課會教 STL 中各種實用的資料結構
這節課先把陣列的基礎打好就好
初始化變數
int a = 10;
初始化陣列
int arr[5] = {10,15,18,13,22};
初始化陣列(每項為 0)
int arr[5] = {0};
元素在陣列中的位置
請記住 從 0 開始
#include <iostream> using namespace std; int main() { int arr[5] = {10,15,18,13,22}; cout << arr[1] << "\n"; return 0; }
輸出結果: 15
#include <iostream> using namespace std; int main() { int arr[5] = {10,15,18,13,22}; cout << arr[5] << "\n"; return 0; }
會產生不預期的結果 請務必注意讀取範圍
#include <iostream> using namespace std; int main() { int arr[5] = {10,15,18,13,22}; a[1] = 20; cout << arr[1] << "\n"; return 0; }
輸出結果:20
學完什麼是陣列以及陣列的索引值
還有讀取與修改陣列元素的方法
接著回到一開始的問題
我們該如何儲存大量的資料呢
或許我們不該宣告一大堆變數然後手動輸入
我們可以使用前面學的迴圈搭配剛學的陣列
來輸入並儲存大量資料
變數的輸入
int a;
cin >> a;
陣列的輸入
int a[5];
for(int i = 0; i < 5; i++) {
cin >> a[i];
}
可以發現我們利用迴圈讓 i 遞增
剛好就可以運用索引值來修改陣列中的每個元素
變數的輸出
cout << a;
陣列的輸出
for(int i = 0; i < 5; i++) {
cout << a[i];
}
與輸入的概念相同 只是把迴圈中的 cin >> 變成 cout << 而已
輸入
3 6 5 8 10 1 4 2 7 9
輸出
1 2 3 4 5 6 7 8 9 10
對於陣列中雜亂無序的整數
可以使用內建函式 sort()
來排序裡面的元素
使用方法:
sort(陣列名稱,陣列名稱 + 元素個數)
#include <iostream> using namespace std; int main() { int arr[10]; for(int i = 0; i < 10; i++) { cin >> arr[i]; } sort(arr, arr + 10); for(int i = 0; i < 10; i++) { cout << arr[i] << " "; } }
sort()函式預設是由小排到大
如果要由大排到小 可以在函式中多加一個參數
// sort(陣列名稱, 陣列名稱 + 元素個數, 降序參數) sort(arr, arr + 10, greater<int>());
初學者使用內建的排序函式就可以了
有興趣的人可以預習一下
排序演算法 by ShiYu's Blog
下學期有可能會教
由 連續字元 所組成 以 " " 包起來
"" // 空字串
"abcdef"
"ABCDEF"
"123456"
有用 " " 包起來的就是字串
#include <bits/stdc++.h> using namespace std; int main() { string s0; // 宣告字串 預設為空字串 string s1 = "abcdef"; string s2 = "ABCDEF"; string s3 = "123456"; }
因為由連續的字元所組成
所以我們也可以用類似陣列的索引值
來存取字串中的單一字元
#include <bits/stdc++.h> using namespace std; int main() { string s = "ABCDEF"; cout << s[0] << s[2] << "\n"; }
可以使用 + 將兩個字串相接起來
#include <bits/stdc++.h> using namespace std; int main() { string s1 = "ABC"; string s2 = "123"; cout << s1 + s2 << "\n"; }
#include <bits/stdc++.h> using namespace std; int main() { string s; char c; while(cin >> c) { s += c; cout << s << "\n"; } }
N NF NFI NFIR NFIRC
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n; while(n != 0) { if(n % 2 == 1) s = "1" + s; else s = "0" + s; n /= 2; } cout << s << "\n"; }
如何讀取一整行字串而不是每次只讀進一個單字呢?
我們可以使用 getline 函數
語法:getline(cin,s)
要計算字串的長度 我們可以使用 size() 函數
#include <bits/stdc++.h> using namespace std; int main() { string s; while(getline(cin,s)) { cout << s.size() << "\n"; } }
用來處理特殊且複雜的輸入格式
詳細使用方法文章
輸入:4 1 8 1 8 8
如何統計每個數字各出現幾次呢?
#include <bits/stdc++.h> using namespace std; int main() { int a[10] = {0}; int n; while(cin >> n) { a[n]++; } for(int i=0;i<10;++i) { cout << a[i] << " "; } }
[c726,c717,a034,a022,a466]
這次題單只有 5 題 但 5 題都是必做題
因為下一次社課的練習賽有一題是這 5 題的綜合版
題解待更新