From 2086ed0d03ee7d9a1441dbacdda5586d511d6c47 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 26 Oct 2022 22:28:42 +0800 Subject: [PATCH] add a base complete quiz 2 cpp code --- .gitignore | 4 ++++ prob2/quiz2.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 .gitignore create mode 100644 prob2/quiz2.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..640a0cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vscode + +**/outfile.txt +**/a.out \ No newline at end of file diff --git a/prob2/quiz2.cpp b/prob2/quiz2.cpp new file mode 100644 index 0000000..ff08992 --- /dev/null +++ b/prob2/quiz2.cpp @@ -0,0 +1,58 @@ +#include + +struct stack { + int obj[6] = {0, 0, 0, 0, 0, 0}; + int next = 0; + int out[6] = {0, 0, 0, 0, 0, 0}; + int out_next = 0; +}; + +void out (stack s) { + std::cout << "{ "; + for (int i = 0; i < s.next; i++) std::cout << s.obj[i] << " "; + std::cout << "// "; + for (int i = 0; i < s.out_next; i++) std::cout << s.out[i] << " "; + std::cout << "}" << std::endl; +} + +void loop (stack s, int next) { + + if (s.out_next == 6) { + for (int i = 0; i < s.out_next; i++) std::cout << s.out[i] << " "; + std::cout << std::endl; + return; + } + + if (next < 7) { + stack sn = stack(s); + sn.obj[sn.next] = next; + sn.next++; + // std::cout << "[DBUG] way IN" << std::endl; + // std::cout << "[DBUG][RA]" << s.out_next << std::endl; + // std::cout << "[DBUG][XM]"; out(sn); + loop(sn, next+1); + } + + if (s.next != 0) { + stack sb = stack(s); + sb.out[sb.out_next] = sb.obj[sb.next-1]; + sb.obj[sb.next-1] = 0; + sb.next--; + sb.out_next++; + // std::cout << "[DBUG] way OUT" << std::endl; + // std::cout << "[DBUG][RA]" << s.out_next << std::endl; + // std::cout << "[DBUG][XM]"; out(sb); + loop(sb, next); + } + +} + +int main () { + + loop (stack(), 1); + + std::cout << "over" << std::endl; + + return 0; + +}