求一算法实现有重复数字的全排列,跪谢~~

来源:百度知道 编辑:UC知道 时间:2024/05/21 10:51:32
我用一个随机函数从0~9中随机的取出M个数字进行全排列,取出来的数字有重复,我的全排列算法无法实现重复数字的全排列,希望有人帮我改下,或者提供别的算法来实现。研究了半天,实在水平有限搞不出来,希望高手能帮忙。
程序如下:
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <time.h>
#include "windows.h"
#include <conio.h>
#define MAX 10 //定义从0到9里随机取数.
int M; //随机取出的数的个数
int deep; //深度
int j=0; //增加一个计数器,跟踪最终排列所在的位置

void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void print_it(int arr[])
{
++j;
ofstream fout("D:/test.txt",ios::app); //将结果以文本输出
fout<<j<<":";
for(int i=0;i<M;i++)
fout<<arr[i]<<" ";
fout<<endl;
}

void perm( int arr[], int deep=0)
{
if(deep==M) // 如果已经排到最后
{
print_it(arr); //

用这个算法吧
#include <algorithm>
#include <iterator>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;
#define MAX 10 //定义从0到9里随机取数.

int main()
{
int M;
cout<<"Input the amounts of the data:";
cin>>M;
srand((unsigned)time(NULL)); //srand()函数产生一个以当前时间开始的随机种子
int *Array=new int[M];
for(int i=0;i<M;i++)
{
Array[i]=rand()%MAX; //MAX为最大值,其随机域为0~MAX-1
cout<<Array[i]<<" ";
}

std::sort(Array, Array+M);
do
{
copy(Array, Array+M, ostream_iterator<float>(cout, " "));
cout << endl;
}while(next_permutation(Array, Array+M));
delete []Array;

return 0;
}

你这程序没有啥问题啊
我运行了,结果是对的啊

#include <