카테고리 없음

TIL (2023/08/23) Wednesday

0to1ton 2023. 8. 23. 10:16

진법 전환

 
// 진법 전환
// 3진법에서 10진법 전환: (10진수 숫자).toString(희망 변환 진법) 

console.log((100).toString(3)); // 10201  10진법 100을 -> 3진법 10201로 전환
console.log((289).toString(4)); // 10201  10-> 4

// 3진수를 10진법 전환: parseInt((숫자), 표현된 진법 값)
console.log(parseInt((100), 3)); // 9 
// 1*3^2 + 0*3^1 + 0*3^0 = 9
console.log(parseInt((10201), 3)); // 100
// 10201 = 1*3^4 + 0*3^3 + 2*3^2 + 0*3^1 + 1*1^0 = 100
console.log(parseInt((10201), 4)); // 289
// 10201 = 1*4^4 + 0*4^3 + 2*4^2 + 0*4^1 + 1*4^0 = 289

 

 

- 알고리즘

- 최대공약수, 최소공배수 구하기

function solution(n, m) {
console.log(n, typeof (n), m, typeof (m)); // 3 number 12 number
 
// n,m 오름차순으로 sorted된 array 형으로 변환
const [min, max] = [n, m].sort((a, b) => a - b);
 
let arr = [];
for (let i = 1; i <= min; i++) {
console.log("i:", i, "min%i:", min, i, min%i, "max%i:", max, i, max%i);
// 최대공약수는 1부터 시작해서 두 정수를 나눈 나머지 값이 0인 경우 배열에 추가 후 배열에서 최대값을 반환
if (min % i === 0 && max % i === 0) {
console.log("=>", i, "min:", min, "max:", max, min % i === 0 && max % i === 0);
arr.push(i)
}
}
console.log(...arr);

// 최소공배수
// 최소공배수는 두 정수의 값을 곱해서 최대공약수로 나눈 값을 이용한 공식을 사용했다.
const GCD = Math.max(...arr);
const LCM = (min * max) / GCD;

 

// push를 이용해서 넣기
return [GCD, LCM] // array
}

t1n = 3;
t1m = 12;

t2n = 2;
t2m = 5;

console.log(solution(t1n, t1m)); // [3,12]
console.log(solution(t2n, t2m)); // [1,10]


// for문 심화 -> while 문으로 변경
// 1. let i 대신에 ;로 사용가능
let count = 0;
for (; count < 5; count++) {
console.log("현재 count 값:", count);
}
console.log("루프 종료 후 count 값:", count);
// 현재 count 값: 0
// 현재 count 값: 1
// 현재 count 값: 2
// 현재 count 값: 3
// 현재 count 값: 4
// 루프 종료 후 count 값: 5


// 2. for 문안에 조건을 걸수있음, 수행연산도 다양하게 걸수있음
let a = 5;
let b = 3;
let r;

for (;
(a !== b);
r = a -b,
a = b,
b = r) {
console.log("a:", a, "b:", b, "r:", r);
}

console.log("루프 종료 후 a:", a, "b:", b);

//for(; 조건; 수행값)
// while(조건){
// 수행값
// }

- 아스키 코드 공부

 
// 아스키 코드 공부
// 알파벳 총 수는 26개 (abcdefghijklmnopqrstuvwxyz)
// ascii -> A 65, Z 90, a 97 z 122
// 아스키 숫자 뽑을 때 : 문자열.charCodeAt(i)
// 아스키 문자 뽑을 때 : String.fromCharCode(97)
let s_sample = "abc"; // 97 98 99
console.log(s_sample.charCodeAt(0)); // 97
console.log(String.fromCharCode(97)); // a

 

 

 
 

- javascript 문법 정리

https://everyonehasadream.tistory.com/9

 

JavaScript 자주 사용하는 활용법 정리

이번 글에서는 JavaScript로 로직, 아이디어를 구현할 때 자유자재로 코딩을 하기 위해 자주 쓰는 구문들을 정리해보려고 한다. 형 변환 number -> string 변환하는 3가지 방법 String(50) -> "50" 50.toString() -

everyonehasadream.tistory.com

 

TIL

- 내일 2주차 시험이다. 이번 주는 알고리즘 week 여서 조건문, 반복문 등을 잘 사용할 수 있는지 테스트 받을 것 같은데 열심히 해봐야겠다!