작성일 :

문제 링크

24075번 - 計算 (Calculation)

설명

두 정수의 합과 차 중 최댓값과 최솟값을 구하는 문제입니다.


접근법

A + B와 A - B 두 값을 계산합니다.

두 값을 비교하여 큰 값을 먼저, 작은 값을 나중에 출력합니다.



Code

C#

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

class Program {
  static void Main() {
    var p = Array.ConvertAll(Console.ReadLine()!.Split(), int.Parse);
    var a = p[0]; var b = p[1];
    var s = a + b;
    var d = a - b;
    if (s >= d) {
      Console.WriteLine(s);
      Console.WriteLine(d);
    } else {
      Console.WriteLine(d);
      Console.WriteLine(s);
    }
  }
}

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; cin >> a >> b;
  int s = a + b;
  int d = a - b;
  if (s >= d) cout << s << "\n" << d << "\n";
  else cout << d << "\n" << s << "\n";

  return 0;
}