[BOJ] 10804번 : 카드 역배치
2024. 8. 12. 14:38ㆍAlgorithm
1. problem :
https://www.acmicpc.net/problem/10804
2. solution 1 :
nums = [i+1 for i in range(20)]
n = 1
while (n > 0):
n -= 1
start_idx,end_idx = map(int,input().split())
nums = nums[:start_idx-1] + list(reversed(nums[start_idx-1:end_idx])) + nums[end_idx:]
print(' '.join(map(str,nums)))
stl reverse를 이용한 풀이다.
3. solution 2 :
#include <bits/stdc++.h>
using namespace std;
int num[21];
// 카드를 역순으로 놓는 함수
void reverse(int a, int b){
for(int i = 0; i < (b - a + 1) / 2; i++)
swap(num[a+i], num[b-i]);
}
int main(void){
ios::sync_with_stdio(0);
cin.tie(0);
for(int i = 1; i <= 20; i++) num[i] = i;
for(int i = 1; i <= 10; i++) {
int a, b;
cin >> a >> b;
reverse(a, b);
}
for(int i = 1; i <= 20; i++) cout << num[i] << ' ';
}
<source code 출처 :https://github.com/encrypted-def/basic-algo-lecture/blob/master/0x02/solutions/10804.cpp >
이 풀이는 reverse라는 함수를 직접 만들어서 푼 풀이다. swap()이라는 함수는 서로 값을 바꿔주는 함수다.
'Algorithm' 카테고리의 다른 글
| [BOJ] 4179번 : 불! (0) | 2024.08.13 |
|---|---|
| [BOJ] 7576번 : 토마토 (0) | 2024.08.13 |
| [BOJ] 1267번 : 핸드폰 요금 (0) | 2024.08.12 |
| [BOJ] 4949번 : 균형잡힌 세상 (0) | 2024.08.08 |
| [BOJ] 6198번 : 옥상 정원 꾸미기 (0) | 2024.08.07 |