※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 6840번 문제인 Who is in the middle?이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/6840 

 

6840번: Who is in the middle?

In the story Goldilocks and the Three Bears, each bear had a bowl of porridge to eat while sitting at his/her favourite chair. What the story didn’t tell us is that Goldilocks moved the bowls around on the table, so the bowls were not at the right seats

www.acmicpc.net

주어지는 세 수중 중간크기의 값을 찾아 출력하는 문제이다.

 

세 수를 배열에 담아두고 std::sort를 이용해 이 수들을 크기순으로 정렬하는 것으로 문제를 간단히 해결할 수 있다.

 

굳이 정렬을 사용하지 않더라도, 어떤 수가 다른 두 수보다 동시에 크지도, 동시에 작지도 않다는 것을 조건문으로 확인해 조건문만으로도 문제를 해결할 수도 있을 것이다.

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <algorithm>
using namespace std;

int arr[3];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	for (int i = 0; i < 3; i++) cin >> arr[i];

	sort(arr, arr + 3);

	cout << arr[1];
}
728x90

+ Recent posts