치춘짱베리굿나이스
[백준] 10104 본문
Party Invitation
문제
You are hosting a party and do not have room to invite all of your friends. You use the following unemotional mathematical method to determine which friends to invite.
Number your friends 1, 2, . . . , K and place them in a list in this order. Then perform m rounds. In each round, use a number to determine which friends to remove from the ordered list.
The rounds will use numbers r1, r2, . . . , rm. In round i remove all the remaining people in positions that are multiples of ri (that is, ri, 2ri, 3ri, . . .) The beginning of the list is position 1.
Output the numbers of the friends that remain after this removal process.
입력
The first line of input contains the integer K (1 ≤ K ≤ 100). The second line of input contains the integer m (1 ≤ m ≤ 10), which is the number of rounds of removal. The next m lines each contain one integer. The ith of these lines (1 ≤ i ≤ m) contains ri (2 ≤ ri ≤ 100) indicating that every person at a position which is multiple of ri should be removed.
출력
The output is the integers assigned to friends who were not removed. One integer is printed per line in increasing sorted order.
풀이
const party = () => {
const fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
const len = parseInt(input[0]);
let arr = Array.from({ length: len }, (n, idx) => idx + 1);
input.shift();
input.shift();
input = input.map((n) => {
return parseInt(n);
});
for (i of input) {
arr = arr.filter((val, idx) => (idx + 1) % i !== 0);
}
console.log(arr.join(" "));
};
party();
반성회
Array.from을 이용하면 길이를 지정하여 인자값이 1씩 커지는 배열을 만들 수 있음
[배열].filter를 이용하면 지정한 조건문이 참이 되게 하는 원소값들만 추려서 새로운 배열을 만들게 해줌
시간이나 메모리 제약 그리고 인자값 개수가 빡빡하게 주어지지 않아서 다행
'Javascript + Typescript > 자바스크립트로 알고리즘풀기' 카테고리의 다른 글
[백준] 17298 (0) | 2022.02.07 |
---|---|
[백준] 3015 (0) | 2022.02.07 |
[백준] 5397 (0) | 2022.02.07 |
[백준] 1158 (0) | 2022.02.07 |
[백준] 10828 (0) | 2022.02.07 |