https://www.acmicpc.net/problem/10104
10104번: Party Invitation
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)
www.acmicpc.net
문제
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.
예제 입력 1 복사
10
2
2
3
예제 출력 1 복사
1
3
7
9
풀이
이 문제를 자바스크립트로 푼 사람이 나포함 3명밖에 없는게 신기하다. 생각보다 오래 걸렸지만 끝내 풀어내서 기쁘다.
주어진 첫번째 숫자는 총 친구들의 수이고 두 번째 숫자는 친구들을 제거할 횟수이다. 그리고 그 뒤로는 두 번째 숫자만큼 입력값을 받는다.
그리고 받은 각각의 입력값의 배수의 자리에 있는 친구들을 없앤다.
예를들어 위의 예제 1에서 친구들이 1,2,3,4,5,6,7,8,9,10이 주어지고 첫 번째는 2의 배수이다. 그래서 1,3,5,7,9만 남고 두 번째 3의 배수일 때는 1,3,7,9가 나온다.
let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
let total = Number(input[0]);
let run = Number(input[1]);
let numArray = [];
for (let i = 0; i <= total; i++) { // 친구들 배열을 만들어준다
numArray.push(i);
}
for (let i = 0; i < run; i++) { // 제거할 횟수만큼 돌려준다
let jump = Number(input[i + 2]); // 점프할 숫자를 가져온다
for (let j = jump; j < numArray.length; j += jump) { // jump할 숫자로 시작해서 나머지 배열도 제거한다.
numArray[j] = 0;
}
for (let k = 1; k < numArray.length; k++) { // 0인 사람들은 없애준다
if (numArray[k] == 0) {
numArray.splice(k, 1);
}
}
}
numArray.splice(0, 1); // 첫번째 0은 없애준다
for (let i = 0; i < numArray.length; i++) {
console.log(numArray[i]);
}
'코딩 문제 > 백준 [ Node.js ]' 카테고리의 다른 글
[ 백준 15654 / Node.js ] N과 M (5) (0) | 2022.09.15 |
---|---|
[ 백준 15652 / Node.js ] N과 M (4) (0) | 2022.09.14 |
[ 백준 15651 / Node.js ] N과 M (3) (0) | 2022.09.13 |
[ 백준 15650 / Node.js ] N과 M (2) (0) | 2022.09.12 |
[ 백준 15649 / Node.js ] N과 M (1) (0) | 2022.09.11 |
댓글