2417 | 정수 제곱근
https://www.acmicpc.net/problem/2417


작성 코드
python - 제곱근 함수 사용
import math
n = int(input())
i = int(math.sqrt(n))
if i**2 < n:
i += 1
print(i)
- sqrt 연산 후 int로 안 묶어주면 소수점 이하도 출력됨
C++ - 제곱근 함수 사용
#include <iostream>
#include<cmath>
using namespace std;
int main() {
long long n;
long long i = 0;
cin >> n;
i = sqrt(n);
if(n > (i * i))
i++;
cout << i;
return 0;
}
- long long i 로 선언했기 때문에 sqrt 연산 시 소수점 이하 출력 X
python - 이분 탐색 활용
n = int(input())
start = 0
end = n
while start <= end:
mid = (start + end) // 2
if mid ** 2 < n:
start = mid + 1
else:
end = mid - 1
print(start)반응형
'Algorithm > BOJ' 카테고리의 다른 글
| [BOJ] 1629 | 곱셈 (0) | 2022.05.23 |
|---|---|
| [BOJ] 17466 | N! mod P (1) (0) | 2022.05.17 |
| [BOJ] 1051 | 숫자 정사각형 (0) | 2022.02.16 |
| [BOJ]1018 | 체스판 다시 칠하기 (0) | 2022.02.14 |
| [BOJ] 2839 | 설탕배달 (0) | 2022.02.11 |
댓글