Zerojudge 基礎題庫a015 矩陣的翻轉


上一題數學題好簡單,這次來難一點,矩陣反轉

簡單來說就是把矩陣順時針轉九十度後鏡像
例如:
3 1 2
8 5 4

8 3
5 1
4 2

3 8
1 5
2 4

所以
a[0][0]  a[0][1]  a[0][2]
a[1][0]  a[1][1]  a[1][2]

變成

a[1][0]  a[0][0]
a[1][1]  a[0][1]
a[1][2]  a[0][2]

再變成

a[0][0]  a[1][0]
a[0][1]  a[1][1]
a[0][2]  a[1][2]

所以如果我設一個正常的陣列
b[0][0]  b[0][1]
b[1][0]  b[1][1]
b[2][0]  b[2][1]


1
2
3
4
5
6
a[0][0] = b[0][0]
a[0][1] = b[1][0]
a[0][2] = b[2][0]
a[1][0] = b[0][1]
a[1][1] = b[1][1]
a[1][2] = b[2][1]


剛好兩個數字顛倒(這好像是定義 -w-)

認識了矩陣反轉,來解題目吧~

準備
1.輸入row,column(列&行)
2.造出輸入儲存的陣列 q[row][column]
3.造出輸出的陣列 ans[column][row]

輸出
4.輸入陣列
5.開始反轉
6.輸出陣列

先把準備工作做好:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <iostream>
using namespace std;
int main()
{
    //1.輸入row,column(列&行)
    int row,column;
    cin >> row >> column;

    //2.造出輸入儲存的陣列 q[row][column]
    int q[row][column];

    //3.造出輸出的陣列 ans[column][row]
    int ans[column][row];
}


準備工作容易,但重要
接下來第四步是 - 輸入陣列:
這就有點騷惱了
我超討厭巢狀for迴圈

輸入
3 1 2
8 5 4
等於
3 1 2 8 5 4

要放入
q[0][0]  q[0][1]  q[0][2]
q[1][0]  q[1][1]  q[1][2]

寫超賴程式碼:

1
2
3
4
5
6
cin >> q[0][0];
cin >> q[0][1];
cin >> q[0][2];
cin >> q[1][0];
cin >> q[1][1];
cin >> q[1][2];


使用一個for loop


1
2
3
4
5
6
for (int i = 0; i <= 1; i++)
{
 cin >> q[i][0];
 cin >> q[i][1];
 cin >> q[i][2];
}


使用兩個for loop


1
2
3
4
5
6
7
for (int i = 0; i <= 1; i++)
{
 for (int j = 0; j <= 2; j++)
        {
  cin >> q[i][j];
 }
}

如果要輸入進去q[row][column]


1
2
3
4
5
for (int i = 0; i <= row-1; i++){
 for (int j = 0; j <= column-1; j++){
  cin >> q[i][j];
 }
}


來最重要的部份了 - 5.開始反轉
把前面的成果叫出來

1
2
3
4
5
6
ans[0][0] = q[0][0] ;
ans[1][0] = q[0][1] ;
ans[2][0] = q[0][2] ;
ans[0][1] = q[1][0] ;
ans[1][1] = q[1][1] ;
ans[2][1] = q[1][2] ;


使用一個for

1
2
3
4
5
for (int i = 0; i <= 1; i++){
 ans[0][i] = q[i][0];
 ans[1][i] = q[i][1];
 ans[2][i] = q[i][2];
}


使用兩個for

1
2
3
4
5
for (int i = 0; i <= 1; i++){
 for (int j = 0; j <= 2; j++){
  ans[j][i] = q[i][j];
 }
}


使用column,row

1
2
3
4
5
for (int i = 0; i <= row-1; i++){
 for (int j = 0; j <= column-1; j++){
  ans[j][i] = q[i][j];
 }
}





6.輸出陣列
也是一樣,我不寫計算過程了


1
2
3
4
5
6
for (int i = 0; i <= column-1; i++){
 for (int j = 0; j <= row-1; j++){
  cout << ans[i][j] << " ";
 }
 cout << endl;
}


全部合起來!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
using namespace std;
int main()
{
    //1.輸入row,column(列&行)
    int row,column;
    cin >> row >> column;

    //2.造出輸入儲存的陣列 q[row][column]
    int q[row][column];

    //3.造出輸出的陣列 ans[column][row]
    int ans[column][row];

    //4.輸入陣列
    for (int i = 0; i <= row-1; i++)
    {
        for (int j = 0; j <= column-1; j++)
        {
            cin >> q[i][j];
        }
    }

    //5.開始反轉
    for (int i = 0; i <= row-1; i++)
    {
        for (int j = 0; j <= column-1; j++)
        {
            ans[j][i] = q[i][j];
        }
    }

    //6.輸出陣列
    for (int i = 0; i <= column-1; i++)
    {
        for (int j = 0; j <= row-1; j++)
        {
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }
}


這樣就完成了!
等等,要不斷地輸入
使用while()
可是有兩個輸入,怎辦?

使用while(1){
cin >> a;
cin >> b;
}

寫成:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;
int main()
{
    while(1)
    {
        //1.輸入row,column(列&行)
        int row,column;
        cin >> row >> column;

        //2.造出輸入儲存的陣列 q[row][column]
        int q[row][column];

        //3.造出輸出的陣列 ans[column][row]
        int ans[column][row];

        //4.輸入陣列
        for (int i = 0; i <= row-1; i++)
        {
            for (int j = 0; j <= column-1; j++)
            {
                cin >> q[i][j];
            }
        }

        //5.開始反轉
        for (int i = 0; i <= row-1; i++)
        {
            for (int j = 0; j <= column-1; j++)
            {
                ans[j][i] = q[i][j];
            }
        }

        //6.輸出陣列
        for (int i = 0; i <= column-1; i++)
        {
            for (int j = 0; j <= row-1; j++)
            {
                cout << ans[i][j] << " ";
            }
            cout << endl;
        }
    }
}


結果:
Killed

那我改成 while(cin >> a){
cin >> b;
}

成果:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;
int main()
{
    //1.輸入row,column(列&行)
    int row,column;

    while(cin >> row >> column)
    {
        //2.造出輸入儲存的陣列 q[row][column]
        int q[row][column];

        //3.造出輸出的陣列 ans[column][row]
        int ans[column][row];

        //4.輸入陣列
        for (int i = 0; i <= row-1; i++)
        {
            for (int j = 0; j <= column-1; j++)
            {
                cin >> q[i][j];
            }
        }

        //5.開始反轉
        for (int i = 0; i <= row-1; i++)
        {
            for (int j = 0; j <= column-1; j++)
            {
                ans[j][i] = q[i][j];
            }
        }

        //6.輸出陣列
        for (int i = 0; i <= column-1; i++)
        {
            for (int j = 0; j <= row-1; j++)
            {
                cout << ans[i][j] << " ";
            }
            cout << endl;
        }
    }
}


結果:
通過檢測

這結果真詭異,我也不知道為什麼?
總之,過了!





留言

這個網誌中的熱門文章

Zerojudge 基礎題庫a004 文文的求婚 (Python)

紙蜻蜓的受風面積與紙蜻蜓落地時間的關係 #1 [實驗歷程與Python Matplotlib]

Zerojudge 基礎題庫a013 羅馬數字 (Python)