0
点赞
收藏
分享

微信扫一扫

(一)枚举 (二)递归 4070 全排列


4070 全排列

  • ​​AC代码​​
  • ​​解析​​
  • ​​坑​​
  • ​​注意格式,4070和2748格式不一样​​
  • ​​输出方式​​

AC代码

/**********************************************************************/
/* _ _ __ __ ____ _____ */
/* | | | | | \/ | / ___| | ___| */
/* | | | | | |\/| | | | | |___ */
/* | |_| | | | | | | |___ | |___| */
/* \___/ |_| |_| \____| |_| */
/**********************************************************************/
#include <iostream>
#include <algorithm>//sort
#include <cstring>//strlen
using namespace std;

const int Length = 8;
int permu_arr[Length] = {1,2,3,4,5,6,7,0};
int permu_mid[Length] = {0};
bool permu_used[Length] = {0};
int L = 1;
void permutation(int n)
{
if(n == L)
{
//不能用这种方法输出,否则出现数组地址cout << permu_mid<< endl;
for(int i = 0;i<L;i++ )
{
cout << permu_mid[i] << " ";
}
cout << endl;
return ;//强制退出
}
for(int i = 0;i < L; i++)
{
if(!permu_used[i])
{
permu_mid[n]=permu_arr[i];
permu_used[i] = true;
permutation(n+1);
permu_used[i] = false;
}
}


}

int main()
{
while(1)
{
cin >> L;
if(!L)
{
return 0;
}
permutation(0);
}

}

解析

​​参看openjudge上另一道和这道题很类似的题,2748​​

注意格式,4070和2748格式不一样

输出方式

2748中,输出字符串,因此可以用cout << (字符串数组名)
遇到’\0’停止输出,整型数组还是需要老老实实for循环输出


举报

相关推荐

0 条评论