Permutation Dynamic Programming Like

2137阅读 1评论2011-12-08 neobilly
分类:C/C++

I Reconstruct my code , and implement with only one extra array
  1 #include
  2 using namespace std;
  3 int ShowResult(char *p,int l);
  4 int Permutation(char *p,char *EA, int l, int h);
  5 int Swap(char *p, int a, int b);
  6 int main(){
  7   int A[] = {1,2,3,4};
  8   char C[] = {'A','V','C','D'};
  9   char EC[4] ;
 10   Permutation(C,EC,0,4);
 11   return 0;
 12 }
 13 int Permutation(char *p,char *EA, int l, int h){
 14   if(l == h - 1){
 15     EA[4 - h + l]= p[l];
 16     ShowResult(EA,4);
 17     return 0;
 18   }
 19   for(int i = l; i < h; i ++){
 20     EA[4 - h + l]= p[i];
 21     Swap(p,i,h -1);
 22     Permutation(p,EA,l,h - 1);
 23     Swap(p,h - 1,i );
 24   }
 25   return 0;
 26 }
 27 int Swap(char *p, int a, int b){
 28   char t = p[a];
 29   p[a] = p[b];
 30   p[b] = t;
 31   return 0;
 32 }
 33 int ShowResult(char *p,int l){
 34   for(int i = 0; i < l - 1; i++)
 35     cout<
 36   cout<
 37   return 0;
 38 }

上一篇:CountingInversion a[i] > 3 * a[j]
下一篇:过河问题 通关留念

文章评论