赞
踩
- //1到n的排列
- //递归
- #include <iostream>
- using namespace std;
- void print_permutation(int* A,int n,int cur)
- {
- if (cur==n) //数组填满的话直接打印
- {
- for (int i=0;i<n;i++)
- cout<<A[i]<<",";
- cout<<"\n";
- }
- else
- {
- for (int i=1;i<=n;i++)
- {
- int ok=1;
- for (int j=0;j<cur;j++)
-
- if (A[j]==i) //判断某个数是否使用过避免重复
- {
- ok=0;
- }
- if (ok)
- {
- A[cur]=i;
- print_permutation(A,n,cur+1); //开始递归
- }
-
- }
- }
- }
-
- int main()
- {int n;
- int A[n];
- cout<<"请输入n的值"<<endl;
- cin>>n;
- print_permutation(A,n,0);
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。