관리 메뉴

Mini

[JS] 사용법 예시 정리 본문

JS

[JS] 사용법 예시 정리

Mini_96 2024. 7. 6. 11:13

* 입력받기

datas에 받은후

[idx]으로 한줄씩 빼서 다시넣어야함

// Run by Node.js
const readline = require('readline');

(async () => {
	let rl = readline.createInterface({ input: process.stdin });
	const datas = [];
	for await (const line of rl) {
		datas.push(line)
		if (datas.length === 3) {
			rl.close();			
		}
	}
	const mousesA = datas[1].split(" ").map(Number).sort( (a,b) => a-b );
	const mousesB = datas[2].split(" ").map(Number).sort( (a,b) => a-b );

	solution(mousesA,mousesB);
	process.exit();
})();

 

* \n을 못빼는듯

아래와같이 한줄씩 출력.

// Run by Node.js
const readline = require('readline');

(async () => {
	let rl = readline.createInterface({ input: process.stdin });
	let n,m;
	let ret=[];
	let flag=1;
	
	for await (const line of rl) {
		//console.log(line);
		[n,m]=line.split(" ");
		rl.close();
	}

	
	for(let i =0;i<n;++i){
		if(i%2==0){
				console.log("#".repeat(m));
			}
			else{
				if(flag==1){
					console.log(".".repeat(m-1)+"#");
				}
				else{
					console.log("#"+".".repeat(m-1));
				}
				flag=!flag;
				
			}
	}
	console.log(ret.join(''));
	process.exit();
})();

- 정렬

// Run by Node.js
const readline = require('readline');
function getPointNum (numbers) {
	let result = [0,0];
	let count = new Map(); // 빈도수와 함께 저장
	let answer = [0,0]; // [평가값 - 빈도의합, 대푯값]
	numbers.sort((a,b)=>a-b);
	numbers.forEach((number) => {
		if(!count.get(number)) {
			count.set(number, 1);
		} else {
			count.set(number, count.get(number)+1);
		}
	})
	
	for (let i=numbers[0]-2; i<=numbers[numbers.length-1]-2; i++) {
		let howMany = 0;
		if(count.get(i-2)) { howMany += count.get(i-2); } 
		if(count.get(i-1)) { howMany += count.get(i-1); } 
		if(count.get(i)) { howMany += count.get(i); } 
		if(count.get(i+1)) { howMany += count.get(i+1); } 
		if(count.get(i+2)) { howMany += count.get(i+2); } 
		//console.log("대표값", i, "총빈도", howMany)
		
		if(howMany > answer[0]) { 
			answer[0] = howMany;
			answer[1] = i;
		}
	}
	return answer[1];
}

function solution(N, mice) {
	let A = mice[0];
	let B = mice[1];
	let result1 = getPointNum(A);
	let result2 = getPointNum(B);
	
	console.log(result1, result2);
	console.log(result1 > result2 ? 'good' : 'bad');
	
	return 0;
}

(async () => {
	let rl = readline.createInterface({ input: process.stdin });
	let N = null;
	let mice = [];
	let count = 0;
	
	for await (const line of rl) {
		if(!N) {
			N = Number(line);
		} else {
			mice.push(line.split(' ').map(Number));
			count++;
			if(count == 2) rl.close();
		}
	}
	
	solution(N, mice);
})();

 

-배열을 문자열로 만드는법

 

-배열정렬

내림차순

- 조건에 맞는 원소만 필터하기

 

모든원소에 *2
원소의 총합

 

* dfs

 

* 나누기를 정수로 바꿔줘야함

 

* 이분탐색

 

* 배열생성

[0] * 50
2차원 배열

 

* dp

 

* 배열 swap

'JS' 카테고리의 다른 글

[JS] 환경변수 설정방법  (0) 2024.08.26
[멘토링] 24.8.23.  (0) 2024.08.23
js 입출력  (0) 2024.08.09
[ 개인 회고 ] 24. 7. 21.  (0) 2024.07.21
[JS] 정규표현식  (0) 2024.07.18