[코딩테스트] 프로그래머스 12985, 예상 대진표
2022. 5. 13. 15:20ㆍCodingTest
문제
https://programmers.co.kr/learn/courses/30/lessons/12985
접근
1. 입력 a와 b가 서로 같은 그룹에 있는지 확인
public boolean isFight(int a, int b) {
return (a + 1) / 2 == (b + 1) / 2 ? true : false;
}
2. 같은 그룹에 있지 않다면 절반씩 나누어 다시 저장
a = (a+1)/2;
b = (b+1)/2;
3. 1번 과정이 참일때까지 반복
코드
public class Solution {
public boolean isFight(int a, int b) {
return (a + 1) / 2 == (b + 1) / 2 ? true : false;
}
public int solution(int n, int a, int b)
{
int answer = 1;
while(true) {
if(isFight(a, b)) {
break;
}
a = (a+1)/2;
b = (b+1)/2;
answer++;
}
return answer;
}
}
class SolutionTest {
@Test
void test1() {
Assert.assertEquals(3, new Solution().solution(8, 4, 7));
}
@Test
void test2() {
Assert.assertEquals(3, new Solution().solution(8, 1, 5));
}
@Test
void test3() {
Assert.assertEquals(3, new Solution().solution(8, 2, 8));
}
@Test
void test4() {
Assert.assertEquals(2, new Solution().solution(8, 2, 3));
}
}
'CodingTest' 카테고리의 다른 글
[코딩테스트] 프로그래머스 60057, 문자열 압축 (0) | 2022.07.04 |
---|---|
[코딩테스트] 프로그래머스 12941, 최솟값 만들기 (0) | 2022.05.16 |
[코딩테스트] 프로그래머스 12981, 영어 끝말잇기 (0) | 2022.05.11 |
[코딩테스트] 프로그래머스 87946, 피로도 (0) | 2022.05.03 |
[코딩테스트] 프로그래머스 72441, 메뉴 리뉴얼 (0) | 2022.05.02 |