성능 요약
메모리: 72.5 MB, 시간: 22.85 ms
구분
코딩테스트 연습 > 연습문제
채점결과
정확성: 100.0<br/>합계: 100.0 / 100.0
제출 일자
2024년 06월 22일 00:24:53
문제 설명
<p>x축과 y축으로 이루어진 2차원 직교 좌표계에 중심이 원점인 서로 다른 크기의 원이 두 개 주어집니다. 반지름을 나타내는 두 정수 <code>r1</code>, <code>r2</code>가 매개변수로 주어질 때, 두 원 사이의 공간에 x좌표와 y좌표가 모두 정수인 점의 개수를 return하도록 solution 함수를 완성해주세요.<br> ※ 각 원 위의 점도 포함하여 셉니다.</p>
<hr>
<h5>제한 사항</h5>
<ul> <li>1 ≤ <code>r1</code> < <code>r2</code> ≤ 1,000,000</li> </ul>
<hr>
<h5>입출력 예</h5> <table class="table"> <thead><tr> <th>r1</th> <th>r2</th> <th>result</th> </tr> </thead> <tbody><tr> <td>2</td> <td>3</td> <td>20</td> </tr> </tbody> </table> <hr>
<h5>입출력 예 설명</h5>
<p><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/ce4fa289-79cf-423b-8f9c-57de0c3b642e/%EC%9E%85%EC%B6%9C%EB%A0%A5%20%EC%98%88%20%EC%84%A4%EB%AA%85.png" title="" alt="입출력 예 설명.png"><br> 그림과 같이 정수 쌍으로 이루어진 점은 총 20개 입니다.</p>
출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
풀이
javaclass Solution { public long solution(int r1, int r2) { long answer = 0; for (int i=1; i<=r2; i++) { int start = (int)Math.ceil(Math.sqrt((long)r1*r1 - (long)i*i)); int end = (int)Math.floor(Math.sqrt((long)r2*r2 - (long)i*i)); answer += end - start + 1; } return answer * 4; } }