작성일 :

문제 링크

22015번 - 金平糖 (Konpeito)

설명

세 사람이 같은 개수를 먹으려면 추가로 필요한 최소 개수를 구하는 문제입니다.


접근법

가장 많이 먹은 사람의 개수에 나머지를 맞추면 됩니다.

세 값 중 최댓값을 구하고, 각 값과 최댓값의 차이를 모두 더합니다.



Code

C#

1
2
3
4
5
6
7
8
9
10
using System;

class Program {
  static void Main() {
    var p = Array.ConvertAll(Console.ReadLine()!.Split(), int.Parse);
    var mx = Math.Max(p[0], Math.Max(p[1], p[2]));
    var ans = (mx - p[0]) + (mx - p[1]) + (mx - p[2]);
    Console.WriteLine(ans);
  }
}

C++

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

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

  int a, b, c; cin >> a >> b >> c;
  int mx = max(a, max(b, c));
  int ans = (mx - a) + (mx - b) + (mx - c);
  cout << ans << "\n";

  return 0;
}