코딩테스트 연습/SWEA

[SWEA] 1961. 숫자 배열 회전 d2 (c++)

수기 2022. 5. 24. 15:49

https://swexpertacademy.com/main/solvingProblem/solvingProblem.do

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

2차원 배열 for문 연습문제? 같은 느낌이었다. 공식이 그렇게 어렵진 않았다! 

핵심 코드는 아래 3줄이다

map90[j][n-1 - i] = map[i][j];
map180[n-1 - i][n-1 - j] = map[i][j];
map270[n-1 - j][i] = map[i][j];

 


#include <iostream>
using namespace std;
#define MAX 8
int main(int argc, char** argv)
{
    int T;
    cin >> T;
    for(int t=0;t<T;t++){
        int n;
        int sum = 0;
        cin >> n;
        int map[MAX][MAX] = {0,};
        int map90[MAX][MAX] = {0,};
        int map180[MAX][MAX] = {0,};
        int map270[MAX][MAX] = {0,};
        
        for(int i=0;i<n;i++)for(int j=0;j<n;j++) cin >> map[i][j];
            
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                map90[j][n-1 - i] = map[i][j];
                map180[n-1 - i][n-1 - j] = map[i][j];
                map270[n-1 - j][i] = map[i][j];
            }
        }
        
        cout << "#" << t+1 << "\n";
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cout << map90[i][j];
            }
            cout <<" ";
            for(int j=0;j<n;j++){
                cout << map180[i][j];
            }
            cout <<" ";
            for(int j=0;j<n;j++){
                cout << map270[i][j];
            }
            cout << "\n";
        }
        
    }
    return 0;
}