Coding Problem
[BOJ 2622] 삼각형만들기
Yepchani
2025. 3. 21. 20:00
반응형
문제
삼각형만들기
https://www.acmicpc.net/problem/2622
풀이
설명
주어진 성냥개비로 만들 수 있는 삼각형의 개수를 구하는 문제입니다.
삼각형을 만들려면 가장 작은 변 2개의 길이의 합이 나머지 변의 길이보다 커야 합니다.
예시 코드
function solution() {
const N = Number(input());
return countTriangles(N);
}
function countTriangles(matchsticks) {
let totalTriangles = 0;
for (let a = 1; a <= matchsticks / 3; a++) {
for (let b = a; b <= (matchsticks - a) / 2; b++) {
const c = matchsticks - a - b;
if (a + b > c) totalTriangles++;
}
}
return totalTriangles;
}