zoukankan      html  css  js  c++  java
  • 数全排列问题

    问题:给出一个数,计算出其全排列结果。

    回答:方法一:

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #define maxN 4
    using namespace std;


    int main()
    {       
            int p[maxN] = {-1};
            for(int i=0; i<maxN; ++i)
            {       
                    p[i] = i+1;
            }       
                    
            do      
            {
                    for(int i=0; i<maxN; ++i)
                    {
                            printf("%d ",p[i]);
                    }
                    printf(" ");
            }while(next_permutation(p,p+maxN) );
            return 0;
    }

    方法二:

    #include <stdio.h>
    #include <stdlib.h>
    //#define maxN 10
    #define maxN 4

    void permutation(int n,int arry[],int cur)
    {
            if(cur == n)
            {
                    int i=0;
                    for(; i<n; ++i)
                    {
                            printf("%d ",arry[i]);
                    }
                    printf(" ");
                    return;
            }
            int iwhich=1;
            for(;iwhich<=n;++iwhich)
            {
                    bool bInArry=false;
                    int itmp=0;
                    for(;itmp<cur;++itmp)
                    {
                            if(iwhich == arry[itmp])
                            {
                                    bInArry = true;
                                    break;
                            }
                    }//is in?
                    if(false == bInArry)
                    {
                            arry[cur] = iwhich;
                            permutation(n,arry,cur+1);
                    }
            }
    }

    int main()
    {
            int arry[maxN] = {-1};
            permutation(maxN,arry,0);
            return 0;
    }

  • 相关阅读:
    OpenCV中OpenMP的使用
    四种简单的图像显著性区域特征提取方法-----AC/HC/LC/FT。
    【编程练习】寻找和为定值的多个数
    【编程练习】正整数分解为几个连续自然数之和
    (视频)《快速创建网站》2.1 在Azure上创建网站及网站运行机制
    OpenCV轮廓检测,计算物体旋转角度
    OpenCV 实现哈哈镜效果
    CUDA Cuts: Fast Graph Cuts on the GPU
    Graph Cut and Its Application in Computer Vision
    OpenCV进行图像相似度对比的几种办法
  • 原文地址:https://www.cnblogs.com/benchao/p/4539821.html
Copyright © 2011-2022 走看看