怎么使排列输出(将存储在文本文件中)的序列看起来比较混乱随机
如何使排列输出(将存储在文本文件中)的序列看起来比较混乱随机?
经排列组合输出(存储在文本文件中)的序列中,往往相邻的两个序列都很相似,现在要使其看起来比较混乱随机,给位大神,你们会怎么处理呢?最好能在输出序列时进行混乱随机处理,不过在文本文件中处理也可,希望给出C/C++的示例代码,谢谢了。。。
------解决方案--------------------
没懂了……给定数据的排列组合不是只有一种解吗
或者你想说把排列组合的顺序打乱
那就for一遍,每次任意选两组解交换
用srand实现就行
不过我还是不知道是不是这个意思,也没法贴代码
------解决方案--------------------
随机重排下标数组,然后按下标数组顺序输出即可。
------解决方案--------------------
srand(time(NULL));
rand
------解决方案--------------------
洗牌。算法参考:
或直接用STL 的
random_shuffle
template<class RanIt>
void random_shuffle(RanIt first, RanIt last);
template<class RanIt, class Fun>
void random_shuffle(RanIt first, RanIt last, Fun& f);
The first template function evaluates swap(*(first + N), *(first + M)) once for each N in the range [1, last - first), where M is a value from some uniform random distribution over the range [0, N). Thus, the function randomly shuffles the order of elements in the sequence.
The second template function behaves the same, except that M is (Dist)f((Dist)N), where Dist is the type iterator_traits::distance_type.
Sample programs: random_shuffle and random_shuffle (predicate version).
------解决方案--------------------
经排列组合输出(存储在文本文件中)的序列中,往往相邻的两个序列都很相似,现在要使其看起来比较混乱随机,给位大神,你们会怎么处理呢?最好能在输出序列时进行混乱随机处理,不过在文本文件中处理也可,希望给出C/C++的示例代码,谢谢了。。。
------解决方案--------------------
没懂了……给定数据的排列组合不是只有一种解吗
或者你想说把排列组合的顺序打乱
那就for一遍,每次任意选两组解交换
用srand实现就行
不过我还是不知道是不是这个意思,也没法贴代码
------解决方案--------------------
随机重排下标数组,然后按下标数组顺序输出即可。
------解决方案--------------------
srand(time(NULL));
rand
------解决方案--------------------
洗牌。算法参考:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int d[6];
int i,n,a,b,t;
int c,j;
void main() {
srand(time(NULL));
printf("shuffle 0..n-1 demo\n");
for (n=1;n<=5;n++) {/* 测试1~5个元素 */
printf("_____n=%d_____\n",n);
j=1;
for (c=1;c<=n;c++) j=j*c;/* j为n! */
j*=n*2;
for (c=1;c<=j;c++) {/* 测试n*2*n!次 */
for (i=0;i<n;i++) d[i]=i;/* 填写0~n-1 */
for (i=n;i>0;i--) {/* 打乱0~n-1 */
a=i-1;b=rand()%i;
if (a!=b) {t=d[a];d[a]=d[b];d[b]=t;}
}
printf("%04d:",c);
for (i=0;i<n;i++) printf("%d",d[i]);
printf("\n");
}
}
printf("shuffle 1..n demo\n");
for (n=1;n<=5;n++) {/* 测试1~5个元素 */
printf("_____n=%d_____\n",n);
j=1;
for (c=1;c<=n;c++) j=j*c;/* j为n! */
j*=n*2;
for (c=1;c<=j;c++) {/* 测试n*2*n!次 */
for (i=1;i<=n;i++) d[i]=i;/* 填写1~n */
for (i=n;i>1;i--) {/* 打乱1~n */
a=i;b=rand()%i+1;
if (a!=b) {t=d[a];d[a]=d[b];d[b]=t;}
}
printf("%04d:",c);
for (i=1;i<=n;i++) printf("%d",d[i]);
printf("\n");
}
}
}
或直接用STL 的
random_shuffle
template<class RanIt>
void random_shuffle(RanIt first, RanIt last);
template<class RanIt, class Fun>
void random_shuffle(RanIt first, RanIt last, Fun& f);
The first template function evaluates swap(*(first + N), *(first + M)) once for each N in the range [1, last - first), where M is a value from some uniform random distribution over the range [0, N). Thus, the function randomly shuffles the order of elements in the sequence.
The second template function behaves the same, except that M is (Dist)f((Dist)N), where Dist is the type iterator_traits::distance_type.
Sample programs: random_shuffle and random_shuffle (predicate version).
------解决方案--------------------
random_shuffle (STL Sample)
The sample code below illustrates how to use the random_shuffle STL function in Visual C++.
Required Header:
<algorithm>
Prototype:
template<class RandomAccessIterator> inline
void random_shuffle(RandomAccessIterator first,
RandomAccessIterator last)
Note: The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.