📄 permut.bak
字号:
//
// PERMUT.CPP
//
// Program that can print the different permutations of an array of elements
// passed (of any size). For example 123456 or abcdef etc.
// To permute ver large numbers you must change the value of MAX_LEN.
//
// Date : 7 april 2002
// Author : Vivek Mohan
// Homepage : http://www.geocities.com/cppresources/
// Email : mailvivek27@sify.com || opendev@phreaker.net
//
//
// Sample Outputs for 123 , 12345 and abcd included in the package
//
//
# include <iostream.h>
# include <string.h>
# include <conio.h>
// Few Prototypes
void rotate_array (char*,int) ;
void print_permutations(char*,int) ;
// Max Number Of Elements
const int MAX_LEN = 10;
// The Counter
int counter = 0;
// Prints the different permutations of the array of elements
// recursively.
void print_permutations(char *array,int pos)
{ char store[MAX_LEN]; //
strcpy(store,array); // Save the array
for(int i=pos;i<strlen(array);++i)
{ rotate_array(array,pos); // Rotate the array
if(pos==strlen(array)-1)
{ cout<<array<<endl;
counter++; // Inc the counter
}
// call print_perm.. recursively
if((pos+1)<strlen(array))
print_permutations(array,pos+1);
}
strcpy(array,store); // Restore the array
}
int main(int argc,char *argv[])
{ if(argc>1)
{ print_permutations(argv[1],0);
cout<<endl<<"Number of permutations = "<<counter<<endl;
}
else
cout<<argv[0]<<" <elements for eg 1234.. or abcd...> ";
}
//
// This function rotates a part of the array
//
void rotate_array(char *array, int pos)
{ char temp = array[pos];
for(int i=pos;i<strlen(array);++i)
{ char temp2 = array[i+1];
array[i+1] = temp;
temp = temp2 ;
}
array[pos] = array[strlen(array)-1];
array[strlen(array)-1] = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -