ֆհαδoᵂ☄ᴷίʆʆer
Знаток
(305)
3 недели назад
#include <iostream>
#include <vector>
#include <string>
void generateParentheses(std::string current, int openRound, int closeRound, int openSquare, int closeSquare, int n) {
if (current.length() == n) {
std::cout << current << std::endl;
return;
}
if (openRound < n / 2) {
generateParentheses(current + '(', openRound + 1, closeRound, openSquare, closeSquare, n);
}
if (closeRound < openRound) {
generateParentheses(current + ')', openRound, closeRound + 1, openSquare, closeSquare, n);
}
if (openSquare < n / 2) {
generateParentheses(current + '[', openRound, closeRound, openSquare + 1, closeSquare, n);
}
if (closeSquare < openSquare) {
generateParentheses(current + ']', openRound, closeRound, openSquare, closeSquare + 1, n);
}
}
int main() {
int n;
std::cout << "Введите число: ";
std::cin >> n;
// Проверка на четность
if (n % 2 != 0) {
std::cout << "Длина должна быть четной." << std::endl;
return 0;
}
generateParentheses("", 0, 0, 0, 0, n);
return 0;
}
на
Входные данные
4
Результат работы
(())
([])
()()
()[]
[()]
[[]]
[]()
[][]