Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
139164 梁思宸 全排列问题 C++ 解答错误 0 0 MS 252 KB 649 2024-03-21 22:56:39

Tests(0/5):


#include <iostream> using namespace std; int n=3 ; int cnt = 0; void Permutation(char *s,int p)//从数组s的第p个位置开始全排列 { if(p==n) { printf("%s\n",s); cnt++; return ; } for(int i=p;i<n;i++) { swap(s[i],s[p]);//每个字符都有成为当前起始字符的机会 Permutation(s,p+1); swap(s[i],s[p]);//返回初始状态,才能保证后面的交换是正确的, 回溯 } } int main() { int n; cin>>n; char s[11]; for (int i = 0; i < n; i++) s[i] = i + '1'; Permutation(s,0); cout<<cnt<<endl; return 0; }


测评信息: