Skip to content

week02 곽혜원 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions week02/week02-곽혜원/12.js
Original file line number Diff line number Diff line change
@@ -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()
19 changes: 19 additions & 0 deletions week02/week02-곽혜원/5.js
Original file line number Diff line number Diff line change
@@ -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);
17 changes: 17 additions & 0 deletions week02/week02-곽혜원/7.js
Original file line number Diff line number Diff line change
@@ -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));