C++陣列

變數

int i;
string s;
  • 不同type有不同大小、分配記憶體
  • 用來儲存東西

輸入一串數字,然後將這串數字倒著輸出去?

ex: 1 2 3 -> 3 2 1

陣列

int a[5];
int a[5] = {1, 2, 3, 5, 6};
int a[] = {3, 4, 5};

陣列

陣列宣告

陣列

陣列使用

cout << a[0] <<"\n";
cout << a[1] <<"\n";
cout << a[2] <<"\n";
a[0] = 6;

陣列

int a[5] = {3, 6, 4, 5, 1};
for(int i = 0; i < 5; i++)
	cout << a[i] <<" ";

陣列:一次allocate 5個連續空間

a[0] = 7;//a[index] = value;
idx  0 1 2 3 4
val 3 6 4 5 1

Remark: index從0到n - 1, n 為allocate的個數

Different type

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s[3];
    for(int i = 0; i < 3; i++)
    	cin >> s[i];
    for(int i = 2; i > -1; i--)
    	cout << s[i] << "\n";
    return 0;
}	

如果變數沒有初始值,那初始值可能是任意值

#include <iostream>
using namespace std;
int main()
{
    int a[10];
    for(int i = 0; i < 10; i++)
        cout << a[i] <<"\n";
	return 0;
}

想要讓陣列的所有初始值為 0 ?

#include <iostream>
using namespace std;
int main()
{
    int a[5] = {0, 0, 0, 0, 0};
    for(int i = 0; i < 5; i++)
        cout << a[i] << "\n";
    int b[5] = {};
    for(int i = 0; i < 5; i++)
    	cout << b[i] << "\n";
	return 0;
}

2D array

//syntax int a[Row][COL]
int main(){
 	int arr[2][3] = {
       {9, 8, 7},
       {6, 5, 4}
    };
    return 0;
}
[0][0]: 9 [0][1]: 8 [0][2]: 7
[1][0]: 6 [1][1]: 5 [1][2]: 4

前面負責控制row,後面負責控制column

練習:zerojudge a015. 矩陣的翻轉(有很多組)

練習:zerojudge d481. 矩陣乘法

Remark

  • array在宣告時大小已經固定
    • 訪問到超過的index會出錯
  • 通常int的陣列可以開到1e6~1e7
    • 1e6代表10的六次方
  • 可以用陣列來做狀態記錄
    • a[2] = 0, a[3] = 1

進位制

常見進位制

  • 二進位
  • 十進位
  • 十六進位

當數字達到一定數量(基數base)時,就向前一位進一

 

3小時26分鐘又10秒總共有幾秒?

3 \times 60^2 + 26 \times 60 + 10 \times 1 = 12370

10進位

  • 每一位數最少是0 最多能到9
  • 超過9的話需要進位

個位數代表: 1

十位數代表:10個1也就是10

百位數代表:10個10也就是100

千位數代表:10個100也就是1000

(1234)_{10} = 1 \times 1000 + 2 \times 100 + 3 \times 10 + 4 \times 1\\ (1234)_{10} = 1 \times 10^3 + 2 \times 10^2 + 3 \times 10^1 + 4 \times 10^0\\

60進位

  • 每一單位數字最少是0 最多能到59
  • 超過59的話需要進位

1秒代表: 1

1分鐘:60個1秒也就是60秒

1小時:60分鐘也就是60*60秒

8進位

  • 每一位數最少是0 最多能到7
  • 超過7的話需要進位

右一位數代表: 1

右二代表:8個1也就是8

右三位數代表:8個8也就是64

右四位數代表:8個64也就是512

(1234)_{8} = 1 \times 8^3 + 2 \times 8^2 + 3 \times 8^1 + 4 \times 8^0\\

16進位

  • 每一位數最少是0 最多能到15
  • 超過15的話需要進位

每一位只能用一個字來代替

A: 10                         D:13

B: 11                         E:14
C: 12        F:15

(13D)_{16} = 1 \times 16^2 + 3 \times 16 + D \times 1 = (317)_{10}

請問

(33)_{8} = ( ? )_{10}

49會出現在8進位嗎,該如何表示

2進位

  • 每一位數最少是0 最多能到 1
  • 超過1的話需要進位
1 1
2 10
3 11
4 100
5 101
6 110
7 111
\begin{align*} (10110)_{2} = &1 \times 2^4 + 0 \times 2^3 + 1 \times 2^2 + 1 \times 2 + 0 \times 1 \\ = &(22)_{10} \end{align*}

n進位轉10進位

\begin{align*} (abcde)_{n} &= a \times n^4 + b \times n^3 + c \times n^2 + d\times n + e \times 1\\ &=(?)_{10} \end{align*}

問題: 要把10進位轉成16進位,用長除法 ?

(439)_{10} = ( ? )_{16}
1\times 256 + B \times 16 + 7 \times 1 = (1B7)_{16}
n⁴ n 1
a b c d e
\begin{align*} &439\ /\ 16\ =\ 27\ ...\ 7\\ &27\ /\ 16\ =\ 1\ ...\ 11\ (11對應到B)\\ &1\ /\ 16\ =\ 0\ ...\ 1 \end{align*}

10進位轉n進位

\begin{align*} &(((0 * 16) + 1) * 16 + 11) * 16 + 7\\ &=1 * 16^2 + 11 * 16 + 7 \end{align*}

10進位轉n進位

\begin{align*} (1012)_{10} &= 1 \times 8^3 + 7 \times 8^2 + 6 \times 8^1 + 4 \times 1 \\ 126 &= 1 \times 8^2 + 7 \times 8^1 + 6 \times 1 \implies 4 \\ 15 &= 1 \times 8^1 + 7 \times 1 \implies 6 \\ 1 &= 1\times 1 \implies 7 \\ &\implies 1 \\ (1764)_{8} \end{align*}

總結

進位制  基底可使用的符號

二進位 2 0, 1
八進位 8 0 ~ 7
十進位 10 0 ~ 9
十六進位 16 0 ~ 9, A~F(A=10...F=15)
  • 10進位轉n進位,用長除法,且要倒轉
  • n進位轉10進位公式

回家作業

Made with Slides.com