zoukankan      html  css  js  c++  java
  • n全排列输出和 n个数的组合(数字范围a~b)

    n全排列输出:

    int WPermutation(int num, bool bRepeat)

    num表示num全排列

    bRepeat标志是否产生反复元素的序列。


    int Permutation(int n, int* A, int cur, bool bRepeat) 
    {
    	static int number = 0;
    	if(cur == n)
    	{   
    		number++;
    		for(int i = 0; i< n; i++)
    		{
    			printf("%d ", A[i]);
    		}
    		printf("
    ");
    	}
    	else
    	{
    		for(int i = 1; i <= n; i++)
    		{
    			int ok = 1;
    			for(int j = 0; j < cur; j++)    
    			{
    				if(!bRepeat)
    				{
    					if(A[j] == i)
    					{
    						ok = 0;
    					}
    				}
    			}
    			
    				if(ok)
    				{
    					A[cur] = i;
    					Permutation(n, A, cur + 1, bRepeat);
    				}			
    
    		}
    	}
    
        return number;
    }
    int WPermutation(int num, bool bRepeat)
    {
    	printf("%d permutation(%s): %d ~ %d
    ", num, bRepeat?"repeat mode":"single mode", 1, num);
    	int n = num;
    	int *A = (int*)malloc(n*sizeof(int));
    	memset(A, 0, sizeof(n*sizeof(int)));
    	int cur = 0;
    	int number = Permutation(n, A, cur, bRepeat);
    	delete [] A ;
    	A = NULL;
    	printf("over!
    ");
    	return number;
    
    }


    n个数的组合(数字范围st~en),考虑反复元素:

    int Wpermutation(int st, int en, int n, bool bRepeat);

    n表示n个数字组合

    每一个数字范围:st~en

    bRepeat标志是否产生反复元素的序列。


    int Permutation(int st, int en, int n, int* A, int cur, bool bRepeat)
    {
    	static int number = 0;
    
    	if(cur == n)
    	{   
    		number++;
    		for(int i = 0; i< n; i++)
    		{
    			printf("%d ", A[i]);
    		}
    		printf("
    ");
    	}
    	else
    	{
    		for(int i = st; i <= en; i++)
    		{
    			int ok = 1;
    			for(int j = 0; j < cur; j++)
    			{
    				if(!bRepeat)
    				{
    					if(A[j] == i)
    					{
    						ok = 0;
    					}
    				}
    			}
    
    			if(ok)
    			{
    				A[cur] = i;
    				Permutation(st, en, n, A, cur + 1, bRepeat);			
    			}			
    
    		}
    	}
    
    	return number;
    }
    
    int Wpermutation(int st, int en, int n, bool bRepeat)
    {
    	printf("%d permutation(%s): %d ~ %d
    ", n, bRepeat?"repeat mode":"single mode", st, en);
    	int num = en - st + 1;
    	if(n > num)
    	{
    		bRepeat = true;
    		printf("too many number, to be repeat mode:
    ");
    	}
    
    	int *A = (int*)malloc(n*sizeof(int));
    	memset(A, 0, sizeof(n*sizeof(int)));
    	int cur = 0;
    	int number = Permutation(st, en, n, A, cur, bRepeat);
    	delete [] A ;
    	A = NULL;
    	printf("over!
    ");
    	return number;
    }
    
    

  • 相关阅读:
    Android的数据存储
    Servlet第一天
    JavaScript高级程序设计读书笔记(3)
    Interesting Papers on Face Recognition
    Researchers Study Ear Biometrics
    IIS 发生意外错误 0x8ffe2740
    Father of fractal geometry, Benoit Mandelbrot has passed away
    Computer vision scientist David Mumford wins National Medal of Science
    Pattern Recognition Review Papers
    盒模型bug的解决方法
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4076181.html
Copyright © 2011-2022 走看看