작성일 :

문제 링크

8674번 - Tabliczka

설명

초콜릿을 한 번만 잘라 두 조각의 차이가 최소가 되도록 할 때 그 차이를 구하는 문제입니다.


접근법

어느 한 변이라도 짝수이면 정중앙을 자를 수 있어 차이는 0입니다.

두 변 모두 홀수이면 완전히 반으로 나눌 수 없고, 두 변 중 작은 값이 최소 차이가 됩니다.


Code

C#

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

class Program {
  static void Main() {
    var line = Console.ReadLine()!.Split();
    var a = long.Parse(line[0]);
    var b = long.Parse(line[1]);

    var ans = 0L;
    if ((a % 2 == 1) && (b % 2 == 1)) ans = Math.Min(a, b);

    Console.WriteLine(ans);
  }
}

C++

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

typedef long long ll;

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

  ll a, b; cin >> a >> b;
  ll ans = 0;
  if ((a & 1LL) && (b & 1LL)) ans = min(a, b);
  cout << ans << "\n";

  return 0;
}