백준: 달팽이는 올라가고 싶다

KangDroid·2021년 5월 29일
0

문제

문제 설명

  • 함정 빠지기 쉬운 문제
  • 달팽이는 낮에 A미터 올라갈 수 있다. 하지만, 밤에 잠을 자는 동안 B미터 미끄러진다
    • 이걸 보면, 단순히 총 길이 / (낮 - 밤) 하면 되겠지! 하겠지만..
  • 또, 정상에 올라간 후에는 미끄러지지 않는다. -> 정상에 도착한 날, 미끄러진걸 계산하면 안됨!

알고리즘

  • 정상에 올라간 후에 미끄러지지 않기 때문에 전체 길이에서 b미터를 미리 빼준다.
  • 수식: (total_length - night_b) / (light_a - night_b) 계산
    끝!

코드

#include <iostream>
#include <cstdio>

using namespace std;

int main(void) {
    int light_a;
    int night_b;
    int total_length;
    scanf("%d", &light_a);
    scanf("%d", &night_b);
    scanf("%d", &total_length);
    if ((total_length - night_b) % (light_a - night_b) == 0) {
        printf("%d\n", (total_length - night_b) / (light_a - night_b));
    } else {
        printf("%d\n", (total_length - night_b) / (light_a - night_b) + 1);
    }
    return 0;
}
profile
Student Platform[Backend] Developer

0개의 댓글