diff --git a/BOJ_14888 b/BOJ_14888 new file mode 100644 index 0000000..8d5332a --- /dev/null +++ b/BOJ_14888 @@ -0,0 +1,52 @@ +#include +#include +#include + +using namespace std; + +int n; // 숫자의 개수 +vector numbers; // 숫자 배열 +int add, sub, mul, divi; // 연산자의 개수 +int max_result = -1e9; // 최댓값을 저장할 변수 (충분히 작은 값으로 초기화) +int min_result = 1e9; // 최솟값을 저장할 변수 (충분히 큰 값으로 초기화) + +void dfs(int idx, int current_result, int add, int sub, int mul, int divi) { + // 모든 숫자를 사용했을 경우, 최대값과 최소값 갱신 + if (idx == n) { + max_result = max(max_result, current_result); + min_result = min(min_result, current_result); + return; + } + + // 각 연산자를 사용할 수 있을 때, 재귀적으로 다음 계산을 진행 + if (add > 0) { + dfs(idx + 1, current_result + numbers[idx], add - 1, sub, mul, divi); + } + if (sub > 0) { + dfs(idx + 1, current_result - numbers[idx], add, sub - 1, mul, divi); + } + if (mul > 0) { + dfs(idx + 1, current_result * numbers[idx], add, sub, mul - 1, divi); + } + if (divi > 0) { + dfs(idx + 1, current_result / numbers[idx], add, sub, mul, divi - 1); // 나눗셈은 정수로 + } +} + +int main() { + // 입력 받기 + cin >> n; + numbers.resize(n); + for (int i = 0; i < n; i++) { + cin >> numbers[i]; + } + cin >> add >> sub >> mul >> divi; + + // DFS(백트래킹) 탐색 시작 + dfs(1, numbers[0], add, sub, mul, divi); + + // 결과 출력 + cout << max_result << '\n' << min_result << '\n'; + + return 0; +} diff --git a/BOJ_15665.cpp b/BOJ_15665.cpp new file mode 100644 index 0000000..836f53c --- /dev/null +++ b/BOJ_15665.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +using namespace std; + +int n, m; +vector numbers; +set> result_set; + +void backtrack(vector& result) { + if (result.size() == m) { + result_set.insert(result); // 중복을 피하기 위해 set에 넣음 + return; + } + + for (int i = 0; i < n; i++) { + result.push_back(numbers[i]); + backtrack(result); + result.pop_back(); // 백트래킹을 위해 마지막 원소 제거 + } +} + +int main() { + // 입력 받기 + cin >> n >> m; + numbers.resize(n); + for (int i = 0; i < n; i++) { + cin >> numbers[i]; + } + + // numbers 배열 정렬 + sort(numbers.begin(), numbers.end()); + + // 백트래킹 시작 + vector result; + backtrack(result); + + // 결과 출력 + for (const auto& seq : result_set) { + for (int num : seq) { + cout << num << " "; + } + cout << '\n'; + } + + return 0; +} diff --git a/BOJ_2477.cpp b/BOJ_2477.cpp new file mode 100644 index 0000000..512b967 --- /dev/null +++ b/BOJ_2477.cpp @@ -0,0 +1,46 @@ +#include +#include + +using namespace std; + +int main() { + int k; // 1m^2 당 참외의 개수 + cin >> k; + + vector> edges(6); // 방향과 길이를 저장할 벡터 + + // 방향과 길이 입력 받기 + for (int i = 0; i < 6; i++) { + cin >> edges[i].first >> edges[i].second; + } + + int max_width = 0, max_height = 0; + int max_width_idx = 0, max_height_idx = 0; + + // 큰 직사각형의 가로와 세로를 찾기 + for (int i = 0; i < 6; i++) { + if (edges[i].first == 1 || edges[i].first == 2) { // 동서 방향 (가로) + if (edges[i].second > max_width) { + max_width = edges[i].second; + max_width_idx = i; + } + } else if (edges[i].first == 3 || edges[i].first == 4) { // 남북 방향 (세로) + if (edges[i].second > max_height) { + max_height = edges[i].second; + max_height_idx = i; + } + } + } + + // 작은 직사각형의 가로와 세로 찾기 + int small_width = edges[(max_width_idx + 3) % 6].second; + int small_height = edges[(max_height_idx + 3) % 6].second; + + // 큰 직사각형의 넓이에서 작은 직사각형의 넓이를 빼서 참외밭의 넓이를 구함 + int area = (max_width * max_height) - (small_width * small_height); + + // 참외밭의 넓이에 1m^2 당 참외의 개수인 k를 곱해 참외의 총 개수를 출력 + cout << area * k << "\n"; + + return 0; +} diff --git a/BOJ_2580.cpp b/BOJ_2580.cpp new file mode 100644 index 0000000..22fd394 --- /dev/null +++ b/BOJ_2580.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +int board[9][9]; // 스도쿠 보드 + +// 스도쿠 보드 출력 함수 +void printBoard() { + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + cout << board[i][j] << ' '; + } + cout << '\n'; + } +} + +// 해당 위치에 숫자를 넣을 수 있는지 확인하는 함수 +bool isValid(int row, int col, int num) { + // 가로줄 확인 + for (int i = 0; i < 9; i++) { + if (board[row][i] == num) { + return false; + } + } + + // 세로줄 확인 + for (int i = 0; i < 9; i++) { + if (board[i][col] == num) { + return false; + } + } + + // 3x3 박스 확인 + int startRow = (row / 3) * 3; + int startCol = (col / 3) * 3; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (board[startRow + i][startCol + j] == num) { + return false; + } + } + } + + return true; +} + +// 스도쿠를 푸는 함수 (백트래킹) +bool solveSudoku() { + for (int row = 0; row < 9; row++) { + for (int col = 0; col < 9; col++) { + // 빈 칸(0)을 찾으면 숫자를 넣어본다 + if (board[row][col] == 0) { + for (int num = 1; num <= 9; num++) { + // 유효한 숫자인지 확인하고, 맞다면 숫자를 넣는다 + if (isValid(row, col, num)) { + board[row][col] = num; + + // 재귀적으로 다음 빈칸을 탐색 + if (solveSudoku()) { + return true; + } + + // 실패하면 다시 빈 칸으로 되돌림 + board[row][col] = 0; + } + } + return false; // 가능한 숫자가 없으면 false 리턴 + } + } + } + return true; // 모든 칸을 채우면 true 리턴 +} + +int main() { + // 스도쿠 보드 입력 받기 + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + cin >> board[i][j]; + } + } + + // 스도쿠 풀이 시작 + if (solveSudoku()) { + printBoard(); // 성공하면 보드 출력 + } else { + cout << "No solution exists\n"; // 해답이 없으면 출력 + } + + return 0; +} diff --git "a/PROG_\354\206\214\354\210\230\354\260\276\352\270\260.cpp" "b/PROG_\354\206\214\354\210\230\354\260\276\352\270\260.cpp" new file mode 100644 index 0000000..7270921 --- /dev/null +++ "b/PROG_\354\206\214\354\210\230\354\260\276\352\270\260.cpp" @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +// 소수인지 확인하는 함수 +bool isPrime(int num) { + if (num <= 1) return false; + for (int i = 2; i <= sqrt(num); i++) { + if (num % i == 0) { + return false; + } + } + return true; +} + +// 주어진 숫자로 만들 수 있는 모든 숫자를 찾아 소수를 찾는 함수 +int solution(string numbers) { + set primeNumbers; // 소수를 저장할 set (중복 방지) + + // 모든 자릿수 조합에 대해 순열을 생성 + for (int i = 1; i <= numbers.size(); i++) { + vector select(numbers.size() - i, false); + select.insert(select.end(), i, true); + + do { + string temp = ""; + for (int j = 0; j < numbers.size(); j++) { + if (select[j]) { + temp += numbers[j]; + } + } + + sort(temp.begin(), temp.end()); // 순열을 만들기 위해 정렬 + do { + int num = stoi(temp); // 숫자로 변환 + if (isPrime(num)) { + primeNumbers.insert(num); // 소수라면 set에 삽입 + } + } while (next_permutation(temp.begin(), temp.end())); + } while (next_permutation(select.begin(), select.end())); + } + + return primeNumbers.size(); // 찾은 소수의 개수를 반환 +} + +int main() { + string numbers; + cout << "Enter numbers: "; + cin >> numbers; + + cout << "Number of prime numbers: " << solution(numbers) << endl; + + return 0; +}