본문 바로가기
  • 소소한 개발자 이야기
Software/알고보면 쓸모있는 코딩스킬

How to Rotate array[][] by 90degree

by Siwan_Min 2020. 4. 17.
728x90
 
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
#include <iostream>
#include <algorithm>
 
using namespace std;
int n;
int map[20][20];
 
void rotate(){
    int temp[20][20];
 
    for(int y = 0; y <n; y++){
        for(int x = 0; x < n; x++){
            //temp[y][x] = map[x][n - y - 1]; //왼쪽으로 90도 회전
            temp[y][x] = map[n-x-1][y]; //오른쪽으로 90
        }
    }
 
    for(int y = 0; y < n; y++){
        for(int x = 0; x <n; x++){
            map[y][x] = temp[y][x];
        }
    }
}
 
int main(){
    cin>>n;
    for(int i = 0 ; i < n; i++) {
        for(int j = 0 ; j < n; j++){
            cin>>map[i][j];
        }
    }
    rotate();
 
    for(int i = 0 ; i < n; i++) {
        for(int j = 0 ; j< n; j++){
            cout<<map[i][j]<<" ";
        }cout<<endl;
    }
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

배열을 시계 방향으로 90도 회전하는 방법입니다. 

728x90

댓글