CSS&JS/코딩테스트

[JS100제]95번 도장찍기

arancia_ 2021. 12. 2. 10:15

문제 링크 : https://www.notion.so/95-b86af91cf9914a84947e017dec5101bc

 

문제95

A new tool for teams & individuals that blends everyday work apps into one.

www.notion.so

기본 답안 링크 : https://www.notion.so/63b6d5e084a64316a0999d2960c0cb98

 

답안

// 기본 입력부분입니다 // N, stmp, k를 입력받습니다. let N = parseInt(prompt('도장의 크기를 입력하세요.'), 10); let stmp = []; // stmp = [ // [1,1,1,2], // [2,0,0,0], // [1,1,1,1], // [0,0,0,0]]; for (let i = 0; i < N; i++){ let

www.notion.so

내가 짠 코드

 

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
/****
 * 빈 종이에 stamp 모양으로 생긴 도장을 90*k 도로 회전하며 찍습니다. 
 * 도장은 N*N 크기이며 
 * 각 도장이 찍히는 부분은 1 이상의 자연수와 도장이 찍히지 않는 부분은 0으로 이루어져 있습니다.
 * 
 * 도장의 크기 N과,
 * 종이에 찍힌 도장 횟수를 표현한 stmp 배열과,
 * 얼마만큼 회전할 것인지를 알려주는 회전수 k를 입력받았을 때 
 * 각 위치에서 몇 번 도장이 찍혔는지 그 결과값을 출력하세요.
 * 
 * 입력
 * 도장 = [[1,1,1,2],[2,0,0,0],[1,1,1,1],[0,0,0,0]]
 * 회전 = 1
 * 
 * 출력
 * [[1, 2, 3, 3], [2, 1, 0, 1], [1, 2, 1, 2], [0, 1, 0, 2]]
 * ****/
 
const stamp = [
    [1,1,1,2],
    [2,0,0,0],
    [1,1,1,1],
    [0,0,0,0]];
const rotate = 1;
 
const test = [
    [1,1,1,1],
    [2,2,2,2],
    [3,3,3,3],
    [4,4,4,4]
];
cs

 

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
46
47
48
49
function copy_array(arr){
    const result = arr
    .map(row =>{
        const thisrow = [];
        const item = row.forEach(col => thisrow.push(col));
        return thisrow;
    });
    return result;
}//copy_array
 
function mySolution(stamp,rotate){
    const per = stamp.length - 1;
    
    let prev = copy_array(stamp);
    let next = copy_array(stamp);
    
    for(let i=0; i<rotate; i++){
        for(let row=0; row<stamp.length; row++){
            for(let col=0; col<stamp.length; col++){
                const prevItem = prev[row][col];
                const nextROW = col;
                const nextCOL = Math.abs(row - per);
                next[nextROW][nextCOL] = prevItem;
            }//for-col
        }//for-row
        prev = copy_array(next);
    }//for-rotate
 
    //중간 결과 확인
    console.log(stamp,next);
 
    //result = 최초 행렬 + 회전후 행렬
    const result = [];
    for(let row = 0; row<stamp.length; row++){
        const new_row = [];
        for(let col=0; col<stamp.length; col++){
            const val_stamp = stamp[row][col];
            const val_next = next[row][col];
            new_row.push(val_stamp + val_next);
        }//for-col;
        result.push(new_row);
    }//for-row
 
    //결과 확인
    console.log(result);
    return result;
}//mySolution
 
mySolution(stamp,rotate);
cs