CS/백준 BOJ
-
[프로그래머스] 자릿수 더하기 JavaScriptCS/백준 BOJ 2022. 4. 29. 02:03
풀이 풀이 1 : String() function solution(n) { var answer = 0; var s = String(n); for (var i = 0; i 0) { answer += Math.floor(n % 10); n /= 10; } return answer; } 문제 문제 설명 자연수 N이 주어지면, N의 각 자릿수의 합을 구해서 return 하는 solution 함수를 만들어 주세요. 예를들어 N = 123이면 1 + 2 + 3 = 6을 return 하면 됩니다..
-
[프로그래머스] 체육복 JavaScriptCS/백준 BOJ 2022. 4. 25. 16:34
풀이 function solution(n, lost, reserve) { // 모든 student가 체육복을 1개씩 갖고 있는 것으로 가정한 배열. var students = new Array(n).fill(1); // 체육복을 잃어버린 student의 체육복 개수를 -1 감소 lost.forEach(l => students[l - 1]--); // 여분 체육복이 있는 student의 체육복 개수를 +1 증가 reserve.forEach(r => students[r - 1]++); /* 여분 체육복이 있는 student(체육복이 2개 이상인 student)가 체육복이 없는 앞, 뒤 student에게 체육복을 준다. 이 때 앞, 뒤 student 둘 다 체육복이 없는 경우 앞의 student에게 체육복을 ..
-
[프로그래머스] 모의고사 JavaScriptCS/백준 BOJ 2022. 4. 25. 16:07
풀이 function solution(answers) { var a = [1, 2, 3, 4, 5]; var b = [2, 1, 2, 3, 2, 4, 2, 5]; var c = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]; var score = [0, 0, 0]; answers.forEach((v, idx) => { if (v === a[idx % a.length]) score[0]++; if (v === b[idx % b.length]) score[1]++; if (v === c[idx % c.length]) score[2]++; }); var max = Math.max(...score); var answer = []; score.forEach((v, idx) => { if (v === ma..
-
[프로그래머스] K번째수 JavaScriptCS/백준 BOJ 2022. 4. 25. 15:46
풀이 function getNumK(arr, i, j, k) { return arr.slice(i - 1, j).sort((a, b) => a - b)[k - 1]; } function solution(array, commands) { return commands.map(command => getNumK(array, ...command)); } 문제 문제 설명 배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하려 합니다. 예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면 array의 2번째부터 5번째까지 자르면 [5, 2, 6, 3]입니다. 1에서 나온 배열을 정렬하면 [2, 3, 5, 6]입니다. 2에서..
-
[프로그래머스] 소수 만들기 JavaScriptCS/백준 BOJ 2022. 4. 25. 15:12
풀이 // n개(nums.length)의 숫자 중 r개의 숫자를 뽑아 만들 수 있는 조합 구하기 (nCr) function getCombinations(nums, r) { if (r === 1) return nums.map(n => [n]); const results = []; // n개 중 1개를 고정하고(fixed), 재귀를 사용해 (n-1Cr-1)을 구한 조합 배열들에 fixed를 붙여준다 nums.forEach((fixed, idx, arr) => { const rest = arr.slice(idx + 1); //idx + 1번째 원소부터 끝까지 잘라내기 const combs = getCombinations(rest, r - 1); results.push(...combs.map(comb => [f..
-
[C++] 백준 10808번 - 알파벳 개수CS/백준 BOJ 2020. 7. 2. 01:21
https://www.acmicpc.net/problem/10808 10808번: 알파벳 개수 단어에 포함되어 있는 a의 개수, b의 개수, …, z의 개수를 공백으로 구분해서 출력한다. www.acmicpc.net #코드 #include using namespace std; int alph[26]; int main(void) { string s; cin >> s; for (auto c : s) alph[c - 'a']++; for (auto n : alph) cout
-
[C++] 백준 4949번 - 균형잡힌 세상 (스택 응용)CS/백준 BOJ 2020. 6. 28. 22:07
https://www.acmicpc.net/problem/4949 4949번: 균형잡힌 세상 문제 세계는 균형이 잘 잡혀있어야 한다. 양과 음, 빛과 어둠 그리고 왼쪽 괄호와 오른쪽 괄호처럼 말이다. 정민이의 임무는 어떤 문자열이 주어졌을 때, 괄호들의 균형이 잘 맞춰져 있는지 판단 www.acmicpc.net #include #include #include #include using namespace std; stack stck; voidprint_result(int is_balanced) { if (is_balanced) cout