ltx_zero

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

全排列 题目本身没有难度

注意在函数循环里面,是走1-n,不是1-x。可执行的范围是1-n

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int countnum=0;
int ans[100];
bool hashTable[1000];
int n;
void qpl(int x)
{
    /*if(x==n+1)
    {
        for(int j=1;j<=n;j++)
            printf("%d ",ans[j]);
        printf("\n");
        countnum++;
        return;
    }*/
    for(int i=1;i<=n;i++)
    {
        if(hashTable[i]==false)
        {
            if(x==n)
            {
                for(int j=1;j<=n-1;j++)
                    printf("%d ",ans[j]);
                printf("%d\n",i);
                countnum++;
                return ;
            }
            ans[x]=i;
            hashTable[i]=true;
            //printf("x:%d i:%d\n",x,i);
            qpl(x+1);
            hashTable[i]=false;
        }
    }
}
int main()
{
    memset(hashTable,0,sizeof(hashTable));
    memset(ans,0,sizeof(ans));
    scanf("%d",&n);
    qpl(1);
    printf("%d\n",countnum);
    printf("finish");
    return 0;
}
View Code
posted on 2020-01-20 22:22  ltx_zero  阅读(118)  评论(0编辑  收藏  举报