登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

sxnuwhui

生活的法则就在于你选择怎样生活!

 
 
 

日志

 
 

题目17:生成1 ~ n的排列(暴力求解)  

2012-05-20 21:54:23|  分类: 经典编程题 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
方法一、
#include <stdio.h>
void fun(int n,int *A,int cur)
{
    int i,j;
    if(cur==n)             //递归边界
    {
        for(i=0;i<n;i++)
            printf("%d",A[i]);
        printf("\n");
    }
    else for(i=1;i<=n;i++)   //尝试在 A[k] 中填写各种整数i
    {
        int ok=1;
        for(j=0;j<cur;j++)
            if(A[j]==i) ok=0;  //如果 i 已经出现过不能再选
        if (ok)
        {
            A[cur]=i;
            fun(n,A,cur+1);  //递归调用
        }
    }
}

int main()
{
    int n;
    int a[100];
    while (scanf("%d",&n)==1)
    {
        fun(n,a,0);
    }
    return 0;
}

类似浙大何海涛方法,排列存于数组中:
void fun(int n,int *A,int cur)
{
    int i;
    if(cur==n)             //递归边界
    {
        for(i=0;i<n;i++)
            printf("%d",A[i]);
        printf("\n");
    }
    else for(i=cur;i<n;i++)
    {
        int temp = A[cur];
        A[cur]=A[i];
        A[i] = temp;

        fun(n,A,cur+1);  //递归调用

        temp = A[cur];
        A[cur] = A[i];
        A[i] = temp;

    }
}

方法二、
利用C++中STL生成排列
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
    int n,p[10];
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&p[i]);
    sort(p,p+n);
    do
    {
        for(int i=0;i<n;i++) printf("%d",p[i]);
        printf("\n");
    } while (next_permutation(p,p+n));
    return 0;
}

关于C++STL的next_permutation:http://hi.baidu.com/zdchenhn/blog/item/986fa5afecb740c07cd92ab5.html


方法三、
浙大何海涛http://zhedahht.blog.163.com/blog/static/254111742007499363479/
/////////////////////////////////////////////////////////////////////////
// Get the permutation of a string,
// for example, input string abc, its permutation is
// abc acb bac bca cba cab
/////////////////////////////////////////////////////////////////////////
void Permutation(char* pStr)
{
      Permutation(pStr, pStr);
}

/////////////////////////////////////////////////////////////////////////
// Print the permutation of a string,
// Input: pStr   - input string
//        pBegin - points to the begin char of string
//                 which we want to permutate in this recursion
/////////////////////////////////////////////////////////////////////////
void Permutation(char* pStr, char* pBegin)
{
      if(!pStr || !pBegin)
            return;

      // if pBegin points to the end of string,
      // this round of permutation is finished, 
      // print the permuted string
      if(*pBegin == '\0')
      {
            printf("%s\n", pStr);
      }
      // otherwise, permute string
      else
      {
            for(char* pCh = pBegin; *pCh != '\0'; ++ pCh)
            {
                  // swap pCh and pBegin
                  char temp = *pCh;
                  *pCh = *pBegin;
                  *pBegin = temp;

                  Permutation(pStr, pBegin + 1);

                  // restore pCh and pBegin
                  temp = *pCh;
                  *pCh = *pBegin;
                  *pBegin = temp;
            }
      }
}
  评论这张
 
阅读(582)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018