반응형

우선 필자는 배열을 선언하고 입력받은 값을 배열 인덱스에 할당해준 후 for문을 활용하여 출력하는 방식으로
코드를 짰다.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int a;
int arr[3] = {};
for (int tmp = 0; tmp < 3; tmp++)
{
cin >> a;
arr[tmp] = a;
}
sort(arr, arr+3);
for (int b : arr)
{
cout << b << " ";
}
return 0;
}
다른 분들의 풀이를 확인해봤더니 배열을 선언하지 않고도 풀 수 있는 재미있는 방법이 있었다
// Authored by : BaaaaaaaaaaarkingDog
// Co-authored by : -
// http://boj.kr/c58003e0efd546baad2e58671aa8f7f3
#include <bits/stdc++.h>
using namespace std;
int main(void){
ios::sync_with_stdio(0);
cin.tie(0);
int a, b, c; // 입력
cin >> a >> b >> c;
int d, e, f; // 크기 순으로 출력할 세 수
d = min({a,b,c});
f = max({a,b,c});
e = a+b+c-d-f;
cout << d << ' ' << e << ' ' << f;
}
최솟값과 최댓값을 맨 앞과 맨 뒤로 두고
맨 처음 세 수를 더한 거에 최댓값 최솟값을 뺀 값을 중간값으로 설정하는 방식이었다
간단한 문제라도 풀이가 정말 다양하게 나올 수 있구나를 느꼈고
창의적인 풀이라고 생각했다.
참고로 입력값을 배열에 할당해줄 때
cin >> arr[i];
이런식으로 해줘도 무방하다.
반응형
'algorithm > 기초 코드' 카테고리의 다른 글
| [algorithm] 백준 2480번 주사위 세개 (0) | 2025.10.12 |
|---|---|
| [algorithm] 백준 2753번 윤년 (0) | 2025.10.12 |
| [algorithm] 백준 9498번 시험 성적 (0) | 2025.10.12 |
| [algorithm] 백준 10869번 사칙연산 (0) | 2025.10.10 |
| [algorithm] 백준 10171번 고양이 (0) | 2025.10.10 |
