작성일 :

문제 링크

4299번 - AFC 윔블던

설명

두 팀 점수의 합 S와 차 D가 주어질 때, 각 팀의 점수를 구하는 문제입니다.


접근법

득점이 높은 팀을 X, 낮은 팀을 Y라 하면 X + Y = S, X - Y = D를 만족해야 합니다.

이를 풀면 X = (S + D) / 2, Y = (S - D) / 2가 됩니다.

S가 D보다 작거나, S + D 또는 S - D가 홀수이면 정수 해가 존재하지 않습니다.

계산한 Y가 음수가 아니면 X와 Y를 출력하고, 아니면 -1을 출력합니다.



Code

C#

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

class Program {
  static void Main() {
    var line = Console.ReadLine()!.Split();
    var s = int.Parse(line[0]);
    var d = int.Parse(line[1]);

    if (s < d || ((s + d) % 2 != 0) || ((s - d) % 2 != 0)) {
      Console.WriteLine(-1);
      return;
    }

    var x = (s + d) / 2;
    var y = (s - d) / 2;
    if (y < 0) Console.WriteLine(-1);
    else Console.WriteLine($"{x} {y}");
  }
}

C++

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

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

  int s, d; cin >> s >> d;
  if (s < d || ((s + d) & 1) || ((s - d) & 1)) {
    cout << -1 << "\n";
    return 0;
  }
  int x = (s + d) / 2;
  int y = (s - d) / 2;
  if (y < 0) cout << -1 << "\n";
  else cout << x << " " << y << "\n";

  return 0;
}