Posts [BOJ 백준] 10804번 카드 역배치 / C++
Post
Cancel

[BOJ 백준] 10804번 카드 역배치 / C++


Contents



문제


문제를 보시려면 링크를 클릭해주세요.


풀이


코드 1

배열을 이용하여 주어진 구간의 시작과 끝을 입력받아, 구간을 역순으로 swap 해주었습니다.

알고보니 reverse 함수를 쓰면 더 간단하게 할 수 있었네요..^^;


코드 2

벡터를 이용하여 입력받은 구간만큼 reverse 함수로 값을 뒤집어 주었습니다.


소스 코드



코드 1


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <algorithm>
using namespace std;

#define N 21

void section_swap(int arr[], int a, int b){
	
	while(true){
		swap(arr[a],arr[b]);
		
		if(b-a==1||b-a==0)
			break;
		a++;
		b--;
	}
}

int main(int argc, char**argv) {
	
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int arr[N];
	int start, end;
	
	for(int i=0; i<N; i++){
		arr[i] = i;
	}
	
	for(int i=0; i<10; i++){
		cin >> start >> end;
	
		section_swap(arr, start, end);
	}
	
	for(int i=1; i<N; i++){
		cout << arr[i] <<" ";
	}
	
	
	return 0;
}



코드 2


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

#define N 21

int main(int argc, char**argv) {
	
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	vector<int> v(N);
	int start, end;
	
	for(int i=0; i<N; i++){
		v[i]=i;
	}
	
	for(int i=0; i<10; i++){
		cin >> start >> end;
		
		reverse(v.begin() + start, v.begin() + end + 1);
	}
	
	for(int i=1; i<N; i++){
		cout << v[i] <<" ";
	}
	
	
	return 0;
}
This post is licensed under CC BY 4.0 by the author.

[BOJ 백준] 2309번 일곱 난쟁이 / C++

[BOJ 백준] 10808번 알파벳 개수 / C++

Comments powered by Disqus.