[BOJ] 5648번 : 역원소 정렬

2024. 8. 30. 07:13Algorithm

1. problem : 

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

 

2. solution 1 :

#include <bits/stdc++.h>
using namespace std;
int n;
vector<string> v; 

int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0); 
	cin >> n; 
	for (int i = 0; i < n; i++) {
		string x; 
		cin >> x;
		v.push_back(x); 
	}
	vector<long long> nums;
	for (string& s : v) {
		string temp; 
		for (int i = s.size() - 1; i >= 0; i--) {
			if (!(temp.empty() && s[i] == 0)) temp.push_back(s[i]); 
		}
		nums.push_back(stoll(temp));
	}
	sort(nums.begin(), nums.end()); 
	for (int i = 0; i < nums.size(); i++) cout << nums[i] << '\n';
}

string --> long으로 바꾸는 함수: stoll 

string --> int로 바꾸는 함수 : stoi

 

3. solution 2 : 

// Authored by : std-freejia
// Co-authored by : -
// http://boj.kr/c56909c54c4f4e9fb2c987a6eb3b96ee
#include <bits/stdc++.h>
using namespace std;

int n;
string st;
vector<long long> v;

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for(int i = 0; i < n; i++) {
    cin >> st;
    reverse(st.begin(), st.end());
    v.push_back(stoll(st));
  }
  sort(v.begin(), v.end());
  for(auto i : v) cout << i << '\n';
  return 0;
}

source code 출처 : https://github.com/encrypted-def/basic-algo-lecture/blob/master/0x0F/solutions/5648.cpp

 

basic-algo-lecture/0x0F/solutions/5648.cpp at master · encrypted-def/basic-algo-lecture

바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.

github.com

reverse를 이용해 string을 뒤집은 다음, stoll을 이용했다. 다음에 써먹어야겠다. 

'Algorithm' 카테고리의 다른 글

[BOJ] 2910번 : 빈도 정렬  (0) 2024.08.30
[BOJ] 1181번 : 단어 정렬  (0) 2024.08.30
[BOJ] 1914번 : 하노이 탑  (0) 2024.08.29
[BOJ] 11652번 : 카드  (0) 2024.08.29
[BOJ] 11651번 : 좌표 정렬하기 2  (0) 2024.08.29