[백준 25784] Easy-to-Solve Expressions (C#, C++) - soo:bak
작성일 :
문제 링크
25784번 - Easy-to-Solve Expressions
설명
서로 다른 양의 정수 3개가 주어질 때, 세 수 사이의 관계를 판별하는 문제입니다.
접근법
먼저 세 수를 정렬하여 가장 큰 값을 찾습니다.
가장 큰 값이 나머지 두 수의 합이면 1, 곱이면 2, 둘 다 아니면 3을 출력합니다.
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 line = Console.ReadLine()!.Split();
var a = int.Parse(line[0]);
var b = int.Parse(line[1]);
var c = int.Parse(line[2]);
var arr = new int[] { a, b, c };
Array.Sort(arr);
if (arr[0] + arr[1] == arr[2]) Console.WriteLine(1);
else if (arr[0] * arr[1] == arr[2]) Console.WriteLine(2);
else Console.WriteLine(3);
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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 v[3] = {a, b, c};
sort(v, v + 3);
if (v[0] + v[1] == v[2]) cout << 1 << "\n";
else if (v[0] * v[1] == v[2]) cout << 2 << "\n";
else cout << 3 << "\n";
return 0;
}