mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
update code formatter
This commit is contained in:
parent
bc7437044e
commit
60ccf91510
60
.vscode/settings.json
vendored
60
.vscode/settings.json
vendored
@ -2,5 +2,63 @@
|
|||||||
"C_Cpp.clang_format_style": "{ BasedOnStyle: Google, UseTab: Never, IndentWidth: 4, TabWidth: 4, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: true, ColumnLimit: 80, AccessModifierOffset: -3, AlignConsecutiveMacros: true }",
|
"C_Cpp.clang_format_style": "{ BasedOnStyle: Google, UseTab: Never, IndentWidth: 4, TabWidth: 4, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: true, ColumnLimit: 80, AccessModifierOffset: -3, AlignConsecutiveMacros: true }",
|
||||||
"editor.formatOnSave": true,
|
"editor.formatOnSave": true,
|
||||||
"editor.formatOnType": true,
|
"editor.formatOnType": true,
|
||||||
"editor.formatOnPaste": true
|
"editor.formatOnPaste": true,
|
||||||
|
"files.associations": {
|
||||||
|
"cctype": "cpp",
|
||||||
|
"clocale": "cpp",
|
||||||
|
"cmath": "cpp",
|
||||||
|
"cstdarg": "cpp",
|
||||||
|
"cstddef": "cpp",
|
||||||
|
"cstdio": "cpp",
|
||||||
|
"cstdlib": "cpp",
|
||||||
|
"cstring": "cpp",
|
||||||
|
"ctime": "cpp",
|
||||||
|
"cwchar": "cpp",
|
||||||
|
"cwctype": "cpp",
|
||||||
|
"array": "cpp",
|
||||||
|
"atomic": "cpp",
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"bitset": "cpp",
|
||||||
|
"chrono": "cpp",
|
||||||
|
"cinttypes": "cpp",
|
||||||
|
"complex": "cpp",
|
||||||
|
"cstdint": "cpp",
|
||||||
|
"deque": "cpp",
|
||||||
|
"list": "cpp",
|
||||||
|
"unordered_map": "cpp",
|
||||||
|
"unordered_set": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"exception": "cpp",
|
||||||
|
"algorithm": "cpp",
|
||||||
|
"functional": "cpp",
|
||||||
|
"iterator": "cpp",
|
||||||
|
"map": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"memory_resource": "cpp",
|
||||||
|
"numeric": "cpp",
|
||||||
|
"optional": "cpp",
|
||||||
|
"random": "cpp",
|
||||||
|
"ratio": "cpp",
|
||||||
|
"set": "cpp",
|
||||||
|
"string": "cpp",
|
||||||
|
"string_view": "cpp",
|
||||||
|
"system_error": "cpp",
|
||||||
|
"tuple": "cpp",
|
||||||
|
"type_traits": "cpp",
|
||||||
|
"utility": "cpp",
|
||||||
|
"fstream": "cpp",
|
||||||
|
"initializer_list": "cpp",
|
||||||
|
"iomanip": "cpp",
|
||||||
|
"iosfwd": "cpp",
|
||||||
|
"iostream": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"new": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"sstream": "cpp",
|
||||||
|
"stdexcept": "cpp",
|
||||||
|
"streambuf": "cpp",
|
||||||
|
"typeinfo": "cpp",
|
||||||
|
"valarray": "cpp"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,41 +15,45 @@
|
|||||||
|
|
||||||
namespace backtracking {
|
namespace backtracking {
|
||||||
namespace magic_sequence {
|
namespace magic_sequence {
|
||||||
using sequence_t = std::vector<unsigned int>;
|
using sequence_t = std::vector<unsigned int>;
|
||||||
|
|
||||||
void print(const sequence_t& s) {
|
void print(const sequence_t& s) {
|
||||||
for(auto item : s) std::cout << item << " ";
|
for (const auto& item : s) std::cout << item << " ";
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if it's a magic sequence
|
||||||
|
bool is_magic(const sequence_t& s) {
|
||||||
|
for (unsigned int i = 0; i < s.size(); i++) {
|
||||||
|
if (std::count(s.cbegin(), s.cend(), i) != s[i])
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if it's a magic sequence
|
// Filtering of sub-solutions
|
||||||
bool is_magic(const sequence_t& s) {
|
// true if the sub-solution is valid otherwise false
|
||||||
for(unsigned int i = 0; i < s.size(); i++)
|
bool filtering(const sequence_t& s, unsigned int depth) {
|
||||||
if(std::count(s.cbegin(), s.cend(), i) != s[i]) return false;
|
return std::accumulate(s.cbegin(), s.cbegin() + depth,
|
||||||
return true;
|
static_cast<unsigned int>(0)) <= s.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filtering of sub-solutions
|
void solve(sequence_t* s, std::list<sequence_t>* ret, unsigned int depth = 0) {
|
||||||
// true if the sub-solution is valid otherwise false
|
if (depth == s->size()) {
|
||||||
bool filtering(const sequence_t& s, unsigned int depth) {
|
if (is_magic(*s))
|
||||||
return std::accumulate(s.cbegin(), s.cbegin() + depth, static_cast<unsigned int>(0)) <=
|
ret->push_back(*s);
|
||||||
s.size();
|
} else {
|
||||||
}
|
for (unsigned int i = 0; i < s->size(); i++) {
|
||||||
|
(*s)[depth] = i;
|
||||||
void solve(sequence_t& s, std::list<sequence_t>& ret, unsigned int depth = 0) {
|
if (filtering(*s, depth + 1))
|
||||||
if(depth == s.size()) {
|
solve(s, ret, depth + 1);
|
||||||
if(is_magic(s)) ret.push_back(s);
|
|
||||||
} else {
|
|
||||||
for(unsigned int i = 0; i < s.size(); i++) {
|
|
||||||
s[depth] = i;
|
|
||||||
if(filtering(s, depth + 1)) solve(s, ret, depth + 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace magic_sequence
|
} // namespace magic_sequence
|
||||||
|
|
||||||
} // namespace backtracking
|
} // namespace backtracking
|
||||||
|
|
||||||
using namespace backtracking::magic_sequence;
|
using namespace backtracking::magic_sequence;
|
||||||
|
|
||||||
@ -64,11 +68,11 @@ static void test() {
|
|||||||
int main() {
|
int main() {
|
||||||
test();
|
test();
|
||||||
|
|
||||||
for(unsigned int i = 2; i < 12; i++) {
|
for (unsigned int i = 2; i < 12; i++) {
|
||||||
std::cout << "Solution for n = " << i << std::endl;
|
std::cout << "Solution for n = " << i << std::endl;
|
||||||
std::list<sequence_t> r1;
|
std::list<sequence_t>* r1 = new std::list<sequence_t>();
|
||||||
sequence_t s1(i, i);
|
sequence_t* s1 = new sequence_t(i, i);
|
||||||
solve(s1, r1);
|
solve(s1, r1);
|
||||||
for(auto item : r1) print(item);
|
for (const auto& item : *r1) print(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user