题目描述

输出自然数1到n所有不重复的排列,即n的全排列,要求所产生的任一数字序列中不允许出现重复的数字。

代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<iomanip>
using namespace std;
int num=0,a[10001]={0},n,r;
bool b[10001]={0};
int search(int);
int print();
int main()
{
    cin>>n;
    r=n;
    search(1);
}

int search(int k)
{
    int i;
    for(i=1;i<=n;++i)//循环模拟寻找排列的过程;
     if(!b[i])//布尔变量为真;
      {
          a[k]=i;
          b[i]=1;
          if(k==r)print();
            else search(k+1);
          b[i]=0;
      }
}

int print()
{
    num++;
    for(int i=1;i<=r;i++)
      cout<<setw(5)<<a[i];
    cout<<endl;
}

 

相关文章:

  • 2021-09-11
  • 2021-07-19
  • 2021-09-29
  • 2022-12-23
  • 2021-07-31
  • 2021-09-22
  • 2022-01-06
  • 2021-08-04
猜你喜欢
  • 2021-10-16
  • 2021-07-22
  • 2021-08-07
  • 2021-12-20
  • 2021-07-03
  • 2021-08-25
相关资源
相似解决方案