[BOJ/C++] 1427 소트인사이드

GamzaTori·2024년 6월 23일

Algorithm

목록 보기
20/133

시간 복잡도 O(n2)O(n^2)을 허용하므로 정렬을 이용하여 문제를 해결할 수 있습니다.

선택 정렬을 이용해 문제를 해결해보겠습니다`

문자열로 입력받아 각 문자를 숫자로 변환해 벡터로 저장한 뒤 정렬을 수행하면 됩니다.

// boj s5 1427
// 소트인사이드

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	string input;
	cin >> input;

	vector<int> v(input.length(), 0);

	for (int i = 0; i < input.length(); i++)
	{
		v[i] = stoi(input.substr(i, 1));
	}

	for (int i = 0; i < v.size(); i++)
	{
		int max = i;
		for (int j = i+1; j < v.size(); j++)
		{
			if (v[j] > v[max])
				max = j;
		}
		::swap(v[i], v[max]);
	}

	for (int i = 0; i < v.size(); i++)
		cout << v[i];

	return 0;
}
profile
게임 개발 공부중입니다.

0개의 댓글