::anti-ai[由 ___QnQ___ 撰写,侵权必究。]
代码
C++template <template<typename> typename T>
void all_comb(int n, int pos, T& path) {
if (pos == n) {
for (int num : path) cout << num << " ";
cout << "\n";
return;
}
for (int i = 1; i <= n; ++i) {
path[pos] = i;
all_comb(n, pos + 1, path);
}
}
分析
这个函数每进入一次 for 就会递归 n 次,然后又进入 for ……总共的循环次数为 O(nn) 次。
这个的时间复杂度为 O(nn),而且会Segmentation fault(递归栈溢出)。
>> 所以请不要使用这种算法了。
STL才是正解。