有一个int[6]的数组,数组中每个元素是0-255之间任意一个数。
求一种算法能够把这种数组的所有可能的结果遍历出来。
4条回答 默认 最新
狂颜 2019-06-05 12:22关注如果要求输出所有可能得话就没有捷径可走,利用DFS或BFS遍历每种情况即可。
DFS代码如下:#include <iostream> #include <cstdio> #include <string> #include <stack> #include <cstring> #include <vector> #include <queue> #include <set> #include <map> #include <sstream> #include <cmath> #include <algorithm> using namespace std; int len = 6; int low_bound = 0, high_bound = 255; vector<int> table; void dfs(int level) { if (level == len) { // 输出数组table的结果 for (auto i : table) { cout << i << " "; } cout << endl; return; } for (int i = low_bound; i <= high_bound; i++) { table.push_back(i); dfs(level + 1); table.pop_back(); } } int main() { table.resize(0); dfs(0); return 0; }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报