작성일 :

문제 링크

11034번 - 캥거루 세마리2

설명

세 마리 캥거루가 점프할 수 있는 최대 횟수를 구하는 문제입니다.


접근법

두 간격 중 더 큰 간격을 줄여 나가는 것이 최적입니다.

큰 간격에서 한 칸씩 채울 수 있으므로 최대 점프 횟수는 큰 간격에서 1을 뺀 값입니다.



Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;

class Program {
  static void Main() {
    var input = "";
    while ((input = Console.ReadLine()) != null) {
      var line = input.Split();
      var a = int.Parse(line[0]);
      var b = int.Parse(line[1]);
      var c = int.Parse(line[2]);
      var ans = Math.Max(b - a, c - b) - 1;
      Console.WriteLine(ans);
    }
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int a, b, c;
  while (cin >> a >> b >> c) {
    int ans = max(b - a, c - b) - 1;
    cout << ans << "\n";
  }

  return 0;
}