[Week 10] RATIO self review - hanjuuuuuu
Closed this issue · 0 comments
hanjuuuuuu commented
RATIO self review
1. 해결 시도 과정
반복문을 돌려 하나씩 증가시키며 처음에 계산한 승률 값과 달라지는 순간의 증가시킨 값이 답이 될 것이라 생각했습니다.
2. 아이디어
최대 200000000번까지 연승이 가능하므로 상수 MAX로 두어 반복문을 만들었습니다.
3. 코드 설명
#include <iostream>
using namespace std;
long long MAX = 2000000000;
int ratio(long long b, long long a) { //승률계산
return a * 100 / b;
}
int calculate(long long game, long long win) {
// 반복문을 돌며 승률이 바뀌지 않을 경우, -1리턴
if (ratio(game, win) == ratio(game+MAX, win+MAX))
return -1;
// 승률이 바뀔 경우, 답 리턴
for (long long i = 0; i < MAX; i++) {
if (ratio(game, win) != ratio(game + i, win + i))
return i;
}
}
int main() {
int T;
int N, M;
cin >> T;
while (T--) {
cin >> N >> M;
cout << calculate(N, M);
}
return 0;
}
4. 막힌 점 및 개선 사항
반복문을 너무 많이 돌리게끔 해서 시간 초과가 발생한 것 같습니다.
반복문의 횟수를 줄일 수 있게 해야겠습니다.