dh-winternagi 님의 블로그
(19554) Guess the number 본문
https://www.acmicpc.net/problem/19554
단계별로 풀어보기
41단계(인터랙티브와 투 스텝 1) 5번째
이전 문제처럼 숫자 맞추기를 하는데, 이번에는 맞추는 쪽이다.
이분 탐색을 구현하면 된다. 단지 결정 함수가 채점기의 출력에 따라 결정될 뿐이다.

#include <iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int resp, l=1, r, n;
cin >> n;
r= n+1;
while(l<r){
int mid= (l+r)/2;
cout << "? " << mid << endl;
cin >> resp;
if(resp<0){
l= mid+1;
}else if(resp>0){
r= mid;
}else{
l= mid;
r= mid;
}
}
cout << "= " << l;
return 0;
}'백준 (C++) > Solve' 카테고리의 다른 글
| (27312) 운영진에게 설정 짜기는 어려워 (0) | 2026.04.22 |
|---|---|
| (23306) binary는 호남선 (0) | 2026.04.22 |
| (31430) A+B - 투 스텝 (0) | 2026.04.22 |
| (23656) Jack and Jill (0) | 2026.04.22 |
| (30924) A+B - 10 (제2편) (0) | 2026.04.22 |
