[BAEKJOON 4153번 : 직각삼각형]
[문제]

[고찰]
해당 문제의 아이디어는 입력 받은 세 변의 길이 중 최대값을 알아야 하고, 피타고라스의 정리에 따라 직각삼각형이 맞는지를 확인하면 되는 것입니다.
저는 그 아이디어에 충실하게 'std::max'를 통해 최대값을 파악하고 'checkTriangle'이라는 함수를 만들어 피타고라스의 정리가 맞는지를 확인하였습니다.
다른 분들은 어떻게 문제를 풀었는지를 살펴보는데, 다양한 방법들이 존재했습니다.
그러한 방법들도 함께 알아두면 좋을 것 같아서 정리합니다.
[개념]
다른 방법을 알아보기 전, std::max의 개념에 대해 먼저 정리해보고자 합니다.
std::min에 대해서는 따로 정리하지 않지만, 최소값을 구한다는 개념만 다를 뿐 아래 정리와 쓰이는 형태 등은 동일합니다.
[std::max]
- 헤더파일
- <algorithm>
- 함수 원형
// 기본 형태
template<class T>
const T& max (const T& a, const T& b)
// 비교 함수 사용
template <class T, class Compare>
const T& max (const T& a, const T& b, Compare comp);
// 여러 값 비교 (C++11 이후)
templaste <class T>
T max (std::initializer_list<T> il);
// 비교 함수 사용하여 여러 값 비교
templaste <class T, class Compare>
T max (std::initializer_list<T> il, Compare comp);
- 특징
- 클래스, 벡터에서도 사용이 가능합니다.
- 클래스에서 max를 사용할 때는, 'operator <'가 클래스에 재정의 되어있어야 합니다.
- std::max의 기본 오버로드는 내부적으로 a < b 연산을 사용하여 비교하기 때문에, 비교하려는 클래스에 대해 operator < (const MyClass& other) const; 와 같은 형태로 < 연산자가 오버로딩되어 있어야 합니다.
- 예제
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main()
{
// 1. 두 정수 비교
int num1 = 5;
int num2 = 10;
int max_int = std::max(num1, num2);
cout << "Max of " << num1 << " and " << num2 << " is: " << max_int << endl;
// 2. 여러 정수 비교 (C++11 이상)
int max_multi_int = std::max({3, 7, 1, 9, 4});
cout << "Max of {3, 7, 1, 9, 4} is: " << max_multi_int << endl;
// 3. 문자열 비교 (사전 순서)
std::string s1 = "apple";
std::string s2 = "banana";
std::string max_string = std::max(s1, s2);
cout << "Max of '" << s1 << "' and '" << s2 << "' is: " << max_string << endl;
// 4. 사용자 정의 비교 함수 사용 예 (길이가 긴 문자열 찾기)
vector<std::string> words = {"short", "medium", "longest"};
// 람다 함수로 길이가 긴 문자열을 찾는 비교 기준 정의 (두 번째 인자가 더 짧으면 true 반환)
auto compare_length = [](const std::string& a, const std::string& b)
{
return a.length() < b.length(); // 길이가 짧은 것이 "작다"
};
string max_length_word = max(words[0], words[1], compare_length);
cout << "Max length word between '" << words[0] << "' and '"
<< words[1] << "' is: " << max_length_word << endl;
string max_length_from_list = max({words[0], words[1], words[2]}, compare_length);
cout << "Max length word from list is: " << max_length_from_list << endl;
return 0;
}

- 가장 큰 값을 swap하여 가장 큰 변의 길이를 한 변수에 고정하는 방법 : swap 알고리즘 사용
#include <iostream>
using namespace std;
int main()
{
while(1)
{
int x, y, z;
int temp = 0;
cin >> x >> y >> z;
if (x == 0 && y == 0 && z == 0)
return 0;
if (x > y)
{
temp = y;
y = x;
x = temp;
}
if (y > z)
{
temp = z;
z = y;
y = temp;
}
if (z * z = x * x + y * y)
cout << "right\n";
else
cout << "wrong\n";
}
}
- 가장 큰 값을 swap하여 가장 큰 변의 길이를 한 변수에 고정하는 방법 : swap 함수 사용
#include <iostream>
using namespace std;
int main()
{
int x, y, z;
while (1)
{
cin >> x >> y >> z;
if (x == 0 && y == 0 && z == 0)
return 0;
if (x > y)
swap(x, y);
if (y > z)
swap(y, z);
if (z * z == x * x + y * y)
cout << "right\n";
else
cout << "wrong\n";
}
}
- 배열로 세 변의 길이를 입력받고, sort() 함수로 정렬하여 가장 큰 변의 길이를 고정하는 방법
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int arr[3];
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
while (1)
{
for (int i = 0; i < 3; i++)
cin >> arr[i];
if (arr[0] == 0 && arr[1] == 0 && arr[2] == 0)
return 0;
sort(arr, arr + 3);
int x = arr[0] * arr[0];
int y = arr[1] * arr[1];
int z = arr[2] * arr[2];
if (z = x + y)
cout << "right\n";
else
cout << "wrong\n";
}
}
- 벡터로 세 변의 길이를 입력받고, sort() 함수로 정렬하여 가장 큰 변의 길이를 고정하는 방법
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
vector<int> n(3);
while (1)
{
for (auto &k : n)
{
cin >> k;
k *= k;
}
if (n[0] == 0 && n[1] == 0 && n[2] == 0)
return 0;
sort(n.begin(), n.end());
if (n[2] == n[0] + n[1])
cout << "right\n";
else
cout << "wrong\n";
}
}
- 가능한 모든 조건을 if 문 조건으로 받는 방법
#include <iostream>
using namespace std;
int main()
{
int x, y, z;
while (1)
{
cin >> x >> y >> z;
if (x == 0 && y == 0 && z == 0)
return 0;
if ((x * x == y * y + z * z) || (y * y == x * x + z * z) || (z * z == x * x + y * y))
cout << "right\n";
else
cout << "wrong\n";
}
}
[정리]
한 문제를 풀더라도 정말 다양한 방법이 있는 것 같습니다.
그 중에서도 가장 효율적으로 문제를 풀 수 있는 방법을 잘 찾는 것이 중요한 것 같습니다.
[Solution]
#include <iostream>
#include <algorithm>
using namespace std;
void checkTriangle(int a, int b, int c)
{
if (a * a == b * b + c * c)
cout << "right\n";
else
cout << "wrong\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int x, y, z;
int max = 0;
while (1)
{
cin >> x >> y >> z;
if (x == 0 && y == 0 && z == 0)
break;
max = std::max({ x, y, z });
if (max == x)
checkTriangle(x, y, z);
else if (max == y)
checkTriangle(y, x, z);
else if (max == z)
checkTriangle(z, x, y);
}
return 0;
}
'코딩 테스트' 카테고리의 다른 글
| [코딩테스트 16일차] BAEKJOON 3052번 : 나머지 (0) | 2025.05.10 |
|---|---|
| [코딩테스트 15일차] BAEKJOON 5073번, 5597번 (0) | 2025.05.06 |
| [코딩테스트 13일차] BAEKJOON 2953번, 3009번, 3053번 (0) | 2025.05.04 |
| [코딩테스트 12일차] BAEKJOON 2935번 : 소음 (0) | 2025.05.03 |
| [코딩테스트 11일차] BAEKJOON 2525번 : 오븐시계, BAEKJOON 2914번 : 저작권 (0) | 2025.05.01 |