컴퓨터공학

[알고리즘/메모] 이분 탐색 binary search 코드 작성 템플릿

TaeGyeong Lee 2023. 9. 15. 01:38

이분 탐색

필요하신 분 사용하세요.

int left = 0;
int right = LENGTH-1;

while(left <= right){

	int mid = (left + right) / 2;
    
    // 만약 특정한 값을 탐색하는 것이 목적이고, 특정한 값을 찾은 경우
    if(mid == ANSWER){
    	break;
    }

	if(mid > ANSWER){
    	right = mid - 1;
    }
    else {
    	left = mid + 1;
    }
}

 

C++ Fast I/O

C++ Fast I/O 코드도 함께 사용할 일이 있으니 참고하세요.

ios_base::sync_with_stdio(false);
cin.tie(NULL);

 

참고 자료

 

Fast I/O for Competitive Programming - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org