# 陣列 --- 陣列是一種可以儲存多筆資料的工具 可以視為是一般資料型態的延伸 --- 一個可以存五個整數的陣列 ```cpp int a[5];//值未定義 int b[5] = {1,2,3,4,5};//值有定義 int c[5] = {};//值有定義 但填入的值不夠 其餘都填0 ``` --- 使用空格時,不可以用未定義的空間 ```cpp int a[5] = {1,2,3,4,5}; cout << a[3];//ok a[1] = 6;//ok a[2] = a[5];//explode ``` 以5為例,有效空間為0~4 --- 陣列廣泛運用於多筆資料的題目 例:給一個值N\(10<=N<=100\) 後面有N個整數...... ```cpp int n; cin >> n; int a[n]; for(int i = 0;i < n;i++){ cin >> a[i]; } ``` --- 結果程式有時對,有時錯 --- 因為陣列空間在編譯時就已經偵測了 不會依照輸入值的不同而改變 範例的N大於10 小於100 因此直接填入100保證夠用 ```cpp int n; cin >> n; int a[100]; /* 很重要! */ for(int i = 0;i < n;i++){ cin >> a[i]; } ``` --- 當題目近似於表格時可以用二維陣列 例:輸入兩個數字N,M\(10<=N,M<=100\) 有N列資料 每列M個整數 ```cpp int n,m; cin >> n >> m; int a[100][100]; for(int i = 0;i < n;i++){ for(int ii = 0;ii < m;ii++){ cin >> a[i][ii]; } } ``` --- b001 最後倒數 http://tcgs.tc.edu.tw:1218/ShowProblem?problemid=b001 --- b003 資料分組 http://tcgs.tc.edu.tw:1218/ShowProblem?problemid=b003 --- b005 熱門點播 http://tcgs.tc.edu.tw:1218/ShowProblem?problemid=b005 ---- tip ---- 第一個陣列存數字 第二個陣列存第一個陣列位置的數字出現次數 ```cpp a[5]={1,2,1,2,2} b[5]={2,3,2,3,3} ``` 再從第二個陣列找最大值就好了
{"metaMigratedAt":"2023-06-15T04:06:04.405Z","metaMigratedFrom":"YAML","title":"陣列","breaks":true,"slideOptions":"{\"transition\":\"slide\"}","contributors":"[{\"id\":\"9f6a1b41-e592-4580-9e63-5613e2cac6cb\",\"add\":1206,\"del\":34}]"}
    198 views
   Owned this note