Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 페이지이동함수
- react-quill 경고메세지
- finddomnode경고
- react상태관리라이브러리
- react hook
- 컴퓨터네트워크
- @published 프로퍼티 래퍼
- jwt로그아웃
- react modal
- CSS
- zustand
- @observedobject 프로퍼티 래퍼
- jwt프론트
- react fsd
- 동기 함수 내에서 비동기 함수 호출
- react-router-dom
- zustand란
- @environmentobject 프로퍼티 래퍼
- 리렌더링최적화
- react-quill
- 원격저장소 연결
- git
- BFS
- C++
- gitbub desktop
- 리액트최적화
- featured-sliced-design
- 비동기함수
- modal 관리
- accesstoken 재발급
Archives
- Today
- Total
leebaek
[BOJ/5568번카드놓기] c++ / backtracking 본문
문제
주어진 N개의 카드 중 K장을 골라 만들 수 있는 숫자의 개수를 구하는 문제
생각
백트래킹 사용해서 나올 수 있는 카드 조합을 구한 뒤 중복되는 수는 삭제해야겠다 생각함
문제풀이
1.cnt가 K개가 되면, r배열에 있는 카드로 숫자 num을 만듦
1-1.res 벡터 배열에 num을 넣어줌
2.0부터 N까지 사용하지 않은 카드가 나오면
2-1.사용표시 + r배열에 저장
2-2.func(cnt+1)
2-3.사용표시 삭제
3.res배열 정렬 후 중복되는 원소 삭제
4.res.size 출력
코드
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int N, K;
int arr[11], r[5], isused[11];
vector<int> res;
void func(int cnt) {
if ( cnt == K ) {
int num = 0;
for ( int i = 0; i < K; i++ ) {
if ( r[i] > 9 )
num = num*10;
num = num*10 + r[i];
}
res.push_back(num);
return;
}
for ( int i = 0; i < N; i++ ) {
if ( !isused[i] ) {
isused[i] = 1;
r[cnt] = arr[i];
func(cnt+1, i);
isused[i] = 0;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
cin >> K;
for ( int i = 0; i < N; i++ ) cin >> arr[i];
func(0);
sort(res.begin(), res.end());
res.erase(unique(res.begin(), res.end()), res.end());
cout << res.size();
}
'BOJ_C++_PS' 카테고리의 다른 글
[BOJ/13023번ABCDE] c++ / backtracking / dfs (0) | 2023.12.01 |
---|---|
[BOJ/15654번N과M(5)] c++ / backtracking (1) | 2023.11.30 |
[BOJ/15663번N과M(9)] c++ / backtracking (1) | 2023.11.28 |
[BOJ/14888번연산자끼워넣기] c++ / backtracking (1) | 2023.11.26 |
[BOJ/1182번부분수열의합] c++ / backtracking (2) | 2023.11.25 |