From 58b7e53508074d3a3db56b8b5c53618e77d417f6 Mon Sep 17 00:00:00 2001 From: YumYumNyang Date: Wed, 20 Jul 2022 16:22:50 +0900 Subject: [PATCH] =?UTF-8?q?week02=20=EA=B3=BD=ED=98=9C=EC=9B=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../12.js" | 57 +++++++++++++++++++ .../5.js" | 19 +++++++ .../7.js" | 17 ++++++ 3 files changed, 93 insertions(+) create mode 100644 "week02/week02-\352\263\275\355\230\234\354\233\220/12.js" create mode 100644 "week02/week02-\352\263\275\355\230\234\354\233\220/5.js" create mode 100644 "week02/week02-\352\263\275\355\230\234\354\233\220/7.js" diff --git "a/week02/week02-\352\263\275\355\230\234\354\233\220/12.js" "b/week02/week02-\352\263\275\355\230\234\354\233\220/12.js" new file mode 100644 index 0000000..15a5bf4 --- /dev/null +++ "b/week02/week02-\352\263\275\355\230\234\354\233\220/12.js" @@ -0,0 +1,57 @@ +const N = 8; + +function printSolution(board){ + for(let i = 0; i < N; i++){ + let str = "" + for(let j = 0; j < N; j++){ + str += `${board[i][j]} `; + } + console.log(str); + } + console.log(" "); +} + + +function isSafe(board, row, col){ + for(let i = 0; i < col; i++){ + if(board[row][i] === 1){ + return false; + } + } + + for(i = row, j = col; i>=0 && j >=0; i--, j--){ + if(board[i][j]){ + return false; + } + } + + for(i = row, j = col; j >= 0 && i < N; i++, j--){ + if(board[i][j]){ + return false; + } + } + return true; +} + +function solveNQUtil(board, col){ + if(col >= N){ + printSolution(board) + return true + } + let res = false; + for(let i = 0; i < N; i++){ + if(isSafe(board, i, col) == true){ + board[i][col] = 1 + res = solveNQUtil(board, col+1) || res; + board[i][col] = 0 + } + } + return res; +} + +function solveNQ(){ + let board = Array.from({length: N}, () => new Array(N).fill(0)); + solveNQUtil(board, 0); + printSolution(board) +} +solveNQ() \ No newline at end of file diff --git "a/week02/week02-\352\263\275\355\230\234\354\233\220/5.js" "b/week02/week02-\352\263\275\355\230\234\354\233\220/5.js" new file mode 100644 index 0000000..9402528 --- /dev/null +++ "b/week02/week02-\352\263\275\355\230\234\354\233\220/5.js" @@ -0,0 +1,19 @@ +function multiplyLinear(num, by){ + if(by === 1) return num; + return num + multiplyLinear(num, by-1); +} + +function multiplyLog(num, by){ + if(by === 1) return num; + + const res = multiplyLog(num, by/2); + if(by%2 === 0){ + return res + res; + }else{ + return res + res + num; + } +} + +const answer1 = multiplyLinear(5, 8); +const answer2 = multiplyLog(5, 8); +console.log(answer2); \ No newline at end of file diff --git "a/week02/week02-\352\263\275\355\230\234\354\233\220/7.js" "b/week02/week02-\352\263\275\355\230\234\354\233\220/7.js" new file mode 100644 index 0000000..a7c8f3a --- /dev/null +++ "b/week02/week02-\352\263\275\355\230\234\354\233\220/7.js" @@ -0,0 +1,17 @@ +let answer = new Set(); + +function permutation(str, prefix){ + if(str.length === 0){ + answer.add(prefix); + return ; + } + for(let i = 0; i < str.length; i++){ + let tmp = str.substring(0,i) + str.substring(i+1); + permutation(tmp, prefix+str[i]); + } + +} + +permutation("abcdccc", ""); + +Array.from(answer).forEach(str => console.log(str)); \ No newline at end of file