cs/백준

[Python] 백준 13706번: 제곱근

신_이나 2022. 7. 18. 01:47

정말 화나게 하는 문제다. 하지만 모든 문제는 답이 있다. 백준 시스템은 절대 틀리지 않는다. 내가 틀릴뿐

 

 

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(int argc, const char * argv[]) {
    ios_base::sync_with_stdio(0);cin.tie(0);
    
    int n;
    cin >> n;
    
    int start, end, mid;
    start = 0; end = n;

    while(start <= end){
        mid = (start + end)/2;
        
        if(mid*mid == n){
            cout << mid << "\n";
            break;
        }
        
        else if(mid*mid < n){
            start = mid+1;
        }
        
        else{
            end = mid-1;
        }
    }
    
    
    return 0;
}

이 코드는 틀렸다. 런타임 에러가 났다.

 

 

 

다른 사람코드를 가져와 보았다.

 

 

# 이분탐색
n = int(input())
low = 1
high = n

while 1:
    mid = (low + high) // 2
    if mid ** 2 == n:
        print(mid)
        break
    elif mid ** 2 > n:
        high = mid - 1
    elif mid ** 2 < n:
        low = mid + 1

 

뭐가 다른거지?? 파이썬으로 돌려서 그런건가? 웃기지도 않다. ㅋ

나 그냥 math 함수 써서 python으로 돌렸는데 sqrt 말고 isqrt 써야한다고 한다. !