Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- OverTheWire
- SDK upgrade
- mongoDB
- linux
- rpg server
- java
- critical rendering path
- spread 연산자
- springboot
- Google Developer API
- Camera Movement
- react
- Packet Network
- Camera Zoom
- MySQL
- server
- screencapture
- unity
- docker
- css framework
- Unity Editor
- Google Refund
- express
- Digital Ocean
- Git
- nodejs
- draganddrop
- Spring Boot
- Unity IAP
- --watch
Archives
- Today
- Total
우당탕탕 개발일지
[프로그래머스] Level 2. 두 원 사이의 정수쌍 본문
https://school.programmers.co.kr/learn/courses/30/lessons/181187
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
해결 방법
- 제 1사분면에서, 두 원사이 점의 개수를 찾고, x4를 한다.
- 축에 있는 것들이 중복검사되면 안되기때문에, x =1 일때부터 x = r2 일때까지의 1사분면 점만 구한다.
- 작은 원을 기준으로 할 때, 아래 조건을 만족하는 가장 작은 y1을 찾는다. $$ x^2 +y^2 >= r1^2 $$
- 큰 원을 기준으로 할 때, 아래 조건을 만족하는 가장 큰 y2를 찾는다 $$ x^2 + y^2 <= r2^2 $$
- y1 <= y <= y2 만큼이 두 원사이의 점이다.
#include <string>
#include <vector>
#include <cmath>
#include <iostream>
using namespace std;
long long out_min(long long x, long long r){
if(x>r) return 0;
long long y2 = r*r - x*x;
return y2 < 0 ? -1 : ceil(sqrt(y2));
}
long long in_max(long long x, long long r){
long long y2 = r*r - x*x;
return y2 < 0 ? -1 : floor(sqrt(y2));
}
long long solution(int r1, int r2) {
long long answer = 0;
for(int x=1; x <= r2; x++){
long long y1 = out_min(x,r1);
long long y2 = in_max(x,r2);
if(y1 < 0 || y2 <0 || y1>y2) continue;
answer += y2-y1+1;
}
return answer*4;
}
마무리 하며
처음에 y1, y2 , out_min() 과 in_max() 를 int형으로 관리했는데, 테스트에서 뒷문제만 fail이 떳다. 첨엔 로직이 잘못된 줄 알고 헤맸는데, 알고보니 숫자가 너무 커져서 outofbound 에러였다. long long으로 수정하고 문제가 해결되었다. 이제는 변수형도 신경을 써야겠다
'Algorithm(c++) > Level 2' 카테고리의 다른 글
[프로그래머스] Level 2. 연속된 부분 수열 (0) | 2025.02.16 |
---|---|
[프로그래머스] Level 2. 혼자서 하는 틱택토 (2) | 2025.02.15 |
[프로그래머스] Level 2. 아날로그 시계 (0) | 2025.02.12 |
[프로그래머스] Level 2. 당구 연습 (1) | 2025.02.11 |
[프로그래머스] Level 2. 숫자 카드 나누기 (1) | 2025.02.10 |