본문 바로가기
PS(Problem Solving)/백준_BOJ

[백준] 1072번 - 게임 (C++) 문제 및 풀이

by 초코칩프라푸치노 2022. 1. 20.

문제) 백준 - 이분 탐색 - 게임

https://www.acmicpc.net/problem/1072

 

1072번: 게임

김형택은 지금 몰래 Spider Solitaire(스파이더 카드놀이)를 하고 있다. 형택이는 이 게임을 이길 때도 있었지만, 질 때도 있었다. 누군가의 시선이 느껴진 형택이는 게임을 중단하고 코딩을 하기 시

www.acmicpc.net

 

이분 탐색으로 승률이 오를 승수를 계산합니다. 승률이 증가했을 경우 ans 업데이트 후 범위를 줄이는 식으로 이분 탐색을 진행합니다.

 

C++ 소스코드)

#include<bits/stdc++.h>
#define endl "\n"
#define MAX 1000000000
#define INF 987654321
#define MOD 1001
#define ll long long
#define int ll
using namespace std;
typedef pair<int, int> p;
int winRate(int x, int y) {
return (y * 100) / x;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int x, y; cin >> x >> y;
int now = winRate(x, y);
if (now >= 99) {
cout << -1;
return 0;
}
int ans = -1;
int s = 0, e = 1000000000;
while (s <= e) {
int mid = (s + e) / 2;
if (winRate(x + mid, y + mid) > now) {
ans = min(ans, mid);
e = mid - 1;
}
else {
s = mid + 1;
}
}
cout << ans;
return 0;
}
view raw 1072.cpp hosted with ❤ by GitHub
반응형

댓글