๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ์ธ๊ณต๋ถ€/Algorithm

[๋ฐฑ์ค€][Node.js] 2960๋ฒˆ : ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2021. 10. 14.

 

 

Algorithm

-  ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด -

 


 

๋ฌธ์ œ

์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋Š” N๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๋ชจ๋“  ์†Œ์ˆ˜๋ฅผ ์ฐพ๋Š” ์œ ๋ช…ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. 2๋ถ€ํ„ฐ N๊นŒ์ง€ ๋ชจ๋“  ์ •์ˆ˜๋ฅผ ์ ๋Š”๋‹ค.
  2. ์•„์ง ์ง€์šฐ์ง€ ์•Š์€ ์ˆ˜ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค. ์ด๊ฒƒ์„ P๋ผ๊ณ  ํ•˜๊ณ , ์ด ์ˆ˜๋Š” ์†Œ์ˆ˜์ด๋‹ค.
  3. P๋ฅผ ์ง€์šฐ๊ณ , ์•„์ง ์ง€์šฐ์ง€ ์•Š์€ P์˜ ๋ฐฐ์ˆ˜๋ฅผ ํฌ๊ธฐ ์ˆœ์„œ๋Œ€๋กœ ์ง€์šด๋‹ค.
  4. ์•„์ง ๋ชจ๋“  ์ˆ˜๋ฅผ ์ง€์šฐ์ง€ ์•Š์•˜๋‹ค๋ฉด, ๋‹ค์‹œ 2๋ฒˆ ๋‹จ๊ณ„๋กœ ๊ฐ„๋‹ค.

N, K๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, K๋ฒˆ์งธ ์ง€์šฐ๋Š” ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

 

๋ฌธ์ œ ํ’€๊ธฐ

const readline = require("readline");
const rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout,
});
let input;
rl.on("line", function (line) {
	input = line.toString();
}).on("close", function () {
	input = input.split(" ").map((el) => Number(el));
	let arr = new Array(input[0] - 1).fill(0);
	arr = arr.map((el, idx) => (arr[idx] = idx + 2));
	let count = 0;

	while (count <= input[1]) {
		let torf = false;
		count++;
		if (count === input[1]) {
			console.log(arr[0]);
			break;
		}
		let minNum = arr.splice(0, 1);

		for (let i = 0; i < arr.length; i++) {
			if (arr[i] % minNum === 0) {
				count++;
				if (count === input[1]) {
					console.log(arr[i]);
					torf = true;
					break;
				}
				arr[i] = 0;
			}
		}
		if (torf) break;
		arr = arr.filter((el) => el !== 0);
	}
	process.exit();
});

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€