ika 7 jaren geleden
bovenliggende
commit
16b05189d8
5 gewijzigde bestanden met toevoegingen van 82 en 10 verwijderingen
  1. 27 0
      .vscode/settings.json
  2. 16 0
      8/Makefile
  3. 22 5
      8/function.cpp
  4. 4 1
      8/function.h
  5. 13 4
      8/main.cpp

+ 27 - 0
.vscode/settings.json

@@ -0,0 +1,27 @@
+{
+  "files.associations": {
+    "*.tcc": "cpp",
+    "cctype": "cpp",
+    "clocale": "cpp",
+    "cmath": "cpp",
+    "cstdint": "cpp",
+    "cstdio": "cpp",
+    "cstdlib": "cpp",
+    "cwchar": "cpp",
+    "cwctype": "cpp",
+    "exception": "cpp",
+    "initializer_list": "cpp",
+    "iosfwd": "cpp",
+    "iostream": "cpp",
+    "istream": "cpp",
+    "limits": "cpp",
+    "new": "cpp",
+    "ostream": "cpp",
+    "stdexcept": "cpp",
+    "streambuf": "cpp",
+    "system_error": "cpp",
+    "type_traits": "cpp",
+    "typeinfo": "cpp",
+    "utility": "cpp"
+  }
+}

+ 16 - 0
8/Makefile

@@ -0,0 +1,16 @@
+CC = /usr/bin/g++
+CFLAGS := -Wall -O3 -std=c++11
+
+.PHONY: all
+all: main.o func.o
+	$(CC) $(CFLAGS) main.o func.o -o run.exe
+
+func.o: function.cpp function.h
+	$(CC) $(CFLAGS) -c function.cpp -o func.o
+
+main.o: main.cpp function.h
+	$(CC) $(CFLAGS) -c main.cpp -o main.o
+
+.PHONY: clean
+clean:
+	rm -f *.o run.exe

+ 22 - 5
8/function.cpp

@@ -1,11 +1,13 @@
 #include <algorithm>
+#include <iostream>
 #include <vector>
 
 #include "function.h"
 
-using namespace std;
+using std::vector; using std::swap; using std::ostream;
 
-bool isswap(vector<int> &arr, size_t beg, size_t end) {
+// 检查beg与end之间有无重复值
+bool isdiff(vector<int> &arr, size_t beg, size_t end) {
   for (size_t i = beg; i < end; ++i) {
     if (arr[i] == arr[end])
       return false;
@@ -13,16 +15,31 @@ bool isswap(vector<int> &arr, size_t beg, size_t end) {
   return true;
 }
 
-void permutation(vector<int> &arr, size_t pos, size_t len, vector<vector<int>> &res){
+// 递归求全排列并存入vector<vector<int>>中
+void perm_r(vector<int> &arr, size_t pos, size_t len, vector<vector<int>> &res){
   if (pos == len) {
     res.push_back(arr);
     return;
   }
   for (size_t i = pos; i <= len; ++i) {
-    if (isswap(arr, pos, i) == true) {
+    if (isdiff(arr, pos, i) == true) {
       swap(arr[pos], arr[i]);
-      permutation(arr. pos + 1, len, res);
+      perm_r(arr, pos + 1, len, res);
       swap(arr[pos], arr[i]);
     }
   }
 }
+
+// 简化入口
+void permutation(vector<int> &arr, vector<vector<int>> &res) {
+  perm_r(arr, 0, arr.size() - 1, res);
+}
+
+// 打印vector<int>
+ostream &print_arr(const vector<int> &vec, ostream &os) {
+  os << "[";
+  for (const int &n : vec)
+    os << " " << n;
+  os << " ]";
+  return os;
+};

+ 4 - 1
8/function.h

@@ -2,6 +2,9 @@
 #define FUNCTION_H
 
 bool isswap(std::vector<int> &, size_t, size_t);
-void permutation(std::vector<int> &, size_t, size_t, std::vector<std::vector<int>> &);
+void perm_r(std::vector<int> &, size_t, size_t, std::vector<std::vector<int>> &);
+void permutation(std::vector<int> &, std::vector<std::vector<int>> &);
+
+std::ostream &print_arr(const std::vector<int> &vec, std::ostream &os);
 
 #endif

+ 13 - 4
8/main.cpp

@@ -1,11 +1,20 @@
-#include <>
+#include <iostream>
 #include <vector>
-#include <string>
 
 #include "function.h"
 
-using namespace std;
+using std::cin; using std::cout; using std::endl;
+using std::vector;
 
 int main(int argc, char **argv) {
-  
+  vector<int> arr{1, 1, 2, 3};
+  vector<vector<int>> res{};
+  permutation(arr, res);
+  cout << "example:\n";
+  print_arr(arr, cout) << "\n\nresult:\n[\n";
+  for (const auto &vec : res) {
+    cout << " ";
+    print_arr(vec, cout) << "\n";
+  }
+  cout << "]" << endl;
 }