---
###### tags: `課程`
---
陣列-array
===
https://reurl.cc/3LVO7L
----

---
## 一維陣列
----
## 格式
```cpp=
資料型態 陣列名稱[陣列大小];
int arr[10];
int ay[3] = {0, 1, 2};
array<int,10> arr;
```
**陣列的區間是arr[0] ~ arr[n-1]**
<font color="red">**不包括arr[n]**</font>
----
## 輸入輸出
```cpp=
for(int i = 0; i < 10 ; i++)
cin >> arr[i];
for(int i = 0; i < 10 ; i++)
cout << arr[i] << ' ';
cout << endl;
rep(i,n) cin >> arr[i];
rep(i,n) cout << arr[i] << endl;
```
----
## 練習時間!
給定$n$接下來會有$n$個數字
請倒過來輸出
進階:並且輸出有多少大於零的數
----
## CODE
```cpp=
#include<iostream>
using namespace std;
int main(){
int n,cnt = 0;
cin>>n;
int arr[n];
for(int i = 0 ; i< n ; i++){
cin >> arr[i];
if(arr[i] > 0) cnt++;
}
for(int i = n-1 ; i >= 0 ; i--)
cout << arr[i] << ' ';
cout << endl << cnt << endl;
return 0;
}
```
---
## 二維陣列
----
表格是很常見的東西
他就是廣義的陣列
| arr[i][j] | 0 | 1 | 2 |
| -- | -- | -- | --|
| 0 | arr[0][0] | arr[0][1]|arr[0][2]|
| 1 | arr[1][0] | arr[1][1]|arr[1][2]|
| 2 | arr[2][0] | arr[2][1]|arr[2][2]|
| 3 | arr[3][0] | arr[3][1]|arr[3][2]|
**這只是陣列的具體化,不一定要指定長成這樣**
----
```cpp=
int arr[5][10];
for(int i = 0; i < 5; i++)
for(int j = 0; j < 10; j++)
cin>>arr[i][j];
for(int i = 0; i < 5; i++){
for(int j = 0; j < 10; j++)
cout << arr[i][j] << ' ';
cout<<endl;
}
```
**$_{_{提醒我要畫圖(小字小字小字)}}$**
----
## 練習時間
輸出三九乘法表(要用陣列先存起來)
| 3\9 | 1 | 2 | 3 |4|5|...n|
| -- | -- | -- | --|--|--|--|
| 1 | 1 | 2|3|4|5|n|
| 2 | 2 | 4|6|8|10|2n|
| 3 | 3 | 6|9|12|15|3n|
----
```cpp=
int arr[3][9];
for(int i = 0; i < 3; i++)
for(int j = 0; j < 9;j++)
arr[i][j] = i*j;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 9; j++)
cout << arr[i][j] << ' ';
cout<<endl;
}
```
---
## 進階
----
## [ ]運算子
會把 arr[10] 括號內跟括號前的位置合併
-> arr(記憶體位置) + 10
10[arr]也是合法的!
```cpp=
試試看: cout << arr <<endl;
```
----
## 行尾換行
```cpp=
for(int i = 0; i < n;i ++)
cout<<arr[i]<<" \n"[i == n-1];
```
---
## 注意事項
----
- 陣列大小是 0 ~ n-1
- 不要呼叫arr[-1]或arr[n]
- 陣列大小最多通常是$10^8$這麼大
- 不要被二維陣列的行列固定想法(行列可交換)
- 大小不可之後改變
- 初始值未固定
---
## string
字串是C++STL的一種工具
用於處理一串文字 用法則類似一維陣列
----
## 輸入輸出
```cpp=
#include<string>
string str;
cin >> str;
cout << str << endl;
cout << str[0] << endl;
```
---
## 題目們
---
## 區間合
給定$n$代表接下來會有n個數字的數列,給定$l, r$
問數列第$l$項到第$r$項的總和
input:
5 3 1 2 5 4
1 3
output:
6
----
## CODE
```cpp=
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
int l, r, cnt = 0;
cin >> l >> r;
for (int i = l; i <= r; i++)
cnt += arr[i];
cout << cnt << endl;
```
---
## 排序數列
給出n 接下來會有一個數列
請由小到大排序他們之後輸出
input: 5 3 9 1 2 5
output: 1 2 3 5 5 9
----
## CODE
```cpp=
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
int tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
}
}
}
//輸出我就省略了
return 0;
}
```
----
## 進階
```cpp=
int n; cin >> n;
rep(i, n) cin >> arr[i];
sort(arr, arr + n);
rep(i, n) cout << arr[i] << " \n"[i == n - 1];
```
---
## 大數
給定兩個大約$10^{40}$的數字
請輸出他們相加
----
## CODE
```cpp=
int numa[105], numb[105], sum[105];
string a, b;
cin >> a >> b;
for (int i = 0; i < a.size(); i++) numa[i] = a[a.size() - i - 1] - '0';
for (int i = 0; i < b.size(); i++) numb[i] = b[b.size() - i - 1] - '0';
for (int i = 0; i < 105; i++) sum[i] = numa[i] + numb[i];
for (int i = 0; i < 104; i++) sum[i + 1] += sum[i] / 10, sum[i] %= 10;
int siz = 104;
while (!sum[siz]) siz--;
for (int i = siz; i >= 0; i--) cout << sum[i];
cout << endl;
```
---
### 踩地雷
給定五組a, b
表示在一張 5\*5 的地圖上(a, b)有地雷
然後請將地圖顯示出來(有數字那種)
----
```cpp=
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
const int maxn = 7;
int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int arr[maxn][maxn];
int main() {
srand(time(NULL));
int cnt = 0;
rep(i, maxn) arr[i][0] = arr[0][i] = arr[maxn - 1][i] = arr[i][maxn - 1] =
-1;
rep(i, 5) {
int l = (rand() % 5) + 1, r = (rand() % 5) + 1;
while (arr[l][r] != 0) l = (rand() % 5) + 1, r = (rand() % 5) + 1;
arr[l][r] = -2, cnt++;
}
rep(i, maxn) rep(j, maxn) {
int cnt = 0;
if (arr[i][j] == 0) {
rep(k, 8) if (arr[i + dx[k]][j + dy[k]] == -2) cnt++;
arr[i][j] = cnt;
}
}
rep(i, maxn) rep(j, maxn) if (arr[i][j] == -2) cout << " *" << ' ';
else cout << setw(2) << arr[i][j] << " \n"[j == maxn - 1];
return 0;
}
```
---
##
---
int 啊就宣告啊
array(或著是任何字元) 宣告後陣列的名字
[n] 存放的資料個數
{ }每個資料的初值
----

----
宣告一個空間為5的**陣列**(自行設計初值)
並且確保輸入的數字(n<5)與輸出的數字相同
(使用陣列)

---
進階
宣告一個**陣列**
並且確保輸入的數字(n<10000)與輸出的數字相同
(一樣要用陣列)
----
建立一個包含n個元素的陣列(0<n<101),輸入n個數值,顯示所有數值與最大數

----
小明是老師,他想偷懶但他不會寫程式,不管怎樣請幫他寫一個程式:
"先輸入學生的成績,透過陣列儲存成績,計算總成績與平均"
(學生數最大為50)

----

----
```cpp=
#include <iostream>
using namespace std;
int main() {
int a[100] = {}, n, max = 0;
cout << "輸入n: ";
cin >> n;
for (int i = 0; i < n; ++i) {
cout << "輸入第" << i + 1 << "個數: ";
cin >> a[i];
if (a[i] > max) max = a[i];
}
cout << "輸入的數: ";
for (int j = 0; j < n; ++j) {
cout << a[j] << " ";
}
cout << "\n最大數: " << max << "\n";
}
```
----

---
### 二維陣列

###### (我亂編的)
----

如上圖,第一個框框指的是"列"(直的),
第二個框框指的是"行"(橫的)
----
#### 二維陣列初值設定
---
假設要設定3X2的陣列初值 a[3][2]
若少一個數值則自動設定為0
int a[3][2]={{1,2},{3},{5,6}}
對應關係:
| 陣列與值 | 陣列與值 |
|:----:|:--------:|
| a[0][0]=1 | a[0][1]=2 |
| a[1][0]=3 | a[1][1]=0 |
| a[2][0]=5 | a[2][1]=6 |
----
請設定好與題目一樣的初值,並用二維陣列印出與下圖一樣的結果

----
小明又在偷懶,他最近在玩掛機遊戲,下表是他的角色數值和光環效果

小明想花一些貨幣買三個光環(可重複),請寫一個程式來推算購買後的結果
###### (挖鑽效率+n%的算法:300/1+n%)
###### (%的效果為疊加而不是相乘)
###### (輸入1就是指鋒利之光,2就是靈性之光...以此類推)
----


##### (警告(?):若對程式沒有一定程度的話請先做其他題)
----
###### a遊戲公司3首人氣歌曲某季的粗計遊玩次數如下:
| 歌曲 | 6月 | 7月 | 8月 |
| -------- | -------- | -------- | -------- |
| Fracture Ray | 2130000 | 4010000 | 3660000 |
| Grievous Lady | 2710000 | 4630000 | 5000000 |
| Tempestissimo | 4330000 | 4860000 | 2230000 |
###### 建立一個二維陣列儲存資料,並計算每首歌和每個月的總遊玩次數
###### 以及這一季的總遊玩次數

----
```cpp=
#include <iostream>
using namespace std;
int main(){
int a[3][5]={{1,2,3,4,5},{6,7,8},{9}};
for(int i=0;i<5;++i){
for(int j=0;j<3;++j){
cout<<" | "<<a[j][i];
}
cout<<" |\n";
}
}
```
----
```cpp=
#include <iostream>
using namespace std;
int main(){
int a[3][3]={{213,401,366},{271,463,500},{433,486,223}};
int sum_song[3]={},sum_month[3]={},sum;
for(int i=0;i<3;++i){
for(int j=0;j<3;++j){
sum_song[i]+=a[i][j];
sum_month[i]+=a[j][i];
sum+=a[i][j];
}
}
cout<<"FR遊玩次數: "<<sum_song[0]*10000;
cout<<"\nGL遊玩次數: "<<sum_song[1]*10000;
cout<<"\nTT遊玩次數: "<<sum_song[2]*10000;
for(int i=0;i<3;++i){
cout<<endl<<i+6<<"月遊玩次數: "<<sum_month[i]*10000;
}
cout<<"\n總計遊玩次數: "<<sum*10000<<endl;
}
```
----

----

----
{"metaMigratedAt":"2023-06-15T14:56:29.073Z","metaMigratedFrom":"Content","title":"陣列-array","breaks":true,"contributors":"[{\"id\":\"7d4f22ac-9934-417b-aa5e-c76934d4fc98\",\"add\":8024,\"del\":318}]"}