1) 字符串 全排列
int Permute(char inString[])
{
int length = 0;
int i=0;
int *used=NULL;
int *out = NULL;
length = strlen(inString);
out = (char*)malloc( length+1);
if(!out)
return 0;
out[length] = '\0';
used = (int*)malloc( sizeof(int) * length );
if(!used)
return 0;
for(i=0; i<length; i++)
used[i]=0;
DoPermute( inString, out, used, length, 0 );
free(out);
free(used);
return 1;
}
void DoPermute(char in[], char out[], int used[], int length, int recursLev)
{
int i;
if(recursLev == length) // base case
{
printf("%s \n", out);
return;
}
for(i =0; i< length; i++)
{
if(used[i])
continue;
out[recursLev] = in[i];
used[i]=1;
DoPermute(in, out, used, length, recursLev+1);
used[i] = 0;
}
}
2) 字符串 全组合
int Combine(char inString[])
{
int length;
char *out;
length = strlen(inString);
out = (char*)malloc(length+1);
if(!out)
return 0;
DoCombine(inString, out, length, 0,0);
free(out);
return 1;
}
void DoCombine(char in[], char out[], int length, int recursLev,int start)
{
int i;
for(i=start, i<length; i++)
{
out[recursLev]=in[i];
out[recursLev+1] = '\0';
printf("%s \n", out);
if(i<length-1)
DoCombine(in, out, length, recursLev+1, i+1);
}
}
3) 升序 降序
int Endianess(void)
{
int tesNum = 1;
char *ptr =NULL;
ptr = (char*)&testNum;
return(*ptr);
}
int Endianess(void)
{
union
{
int theInteger;
char singleByte;
}endianTest;
endianTest.theInteger = 1;
return endianTest.singleByte;
}