有一个int[6]的数组,数组中每个元素是0-255之间任意一个数。
求一种算法能够把这种数组的所有可能的结果遍历出来。
如果要求输出所有可能得话就没有捷径可走,利用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;
}
#include
#include
#include
int main()
{
int numbers[100], i, j, temp;
srand(time(NULL));
// 产生随机数
for (i = 0; i < 100; i++) {
numbers[i] = rand() % (255 + 1);
}
// 冒泡法排序
for (i = 0; i < (100 - 1); i++) {
for (j = 0; j < (100 - i - 1); j++) {
if (numbers[j] > numbers[j + 1]) {
temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
// 输出
for (i = 0; i < 100; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
system("pause");
return 0;
}
(https://blog.csdn.net/hangsyt108/article/details/82462469 "")
排列组合数
数组 0 1 2 3 4 5
范围 0-255 0-255 0-255 0-255 0-255 0-255
可能情况 256 x 256 x 256 x 256 x 256 x 256
256^6=281474976710656
以无符号byte类型处理
=256^6/(1024^3)=262144G
目前没有这么多内存无法打印,但是打印其中一种是可以的,以及判断是不是其中一种情况
int arr[6];
srand(time(nullptr));
for(int i=0;i<6;++i){
arr[i]=rand()%256;
cout<<arr[i]<<"\t";
}
cout<<endl;