/*
* =====================================================================================
*
* Filename: basedonleftrotate.c
*
* Description:
*
* Version: 1.0
* Created: 09/03/2012 09:54:25 AM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (erliang),
* Organization:
*
* =====================================================================================
*/
/*
* n个互异元素的全排列
* 此解法摘抄至网络luxuery-zh.iteye.com/blog/1669841
* 简单描述:
* 根本思想是:n个互异元素的全排列,n个元素分别放在第一个位置剩下的n-1个元素全排列的排列的并(这里他们之间并没有交集)
* 固定第一个元素将其他元素全排列,然后左旋一位,再固定第一个元素(原第二个元素)其他元素全排列,如此往复直到所有元素都移动到了第一个元素位置一次。
* */
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#define swap(a,b) {(a)=(a)^(b);(b)=(a)^(b);(a)=(a)^(b);}
static int *leftRotate(int *pa,int len);
/*
* func describe:array left rotate one place
* in:
* pa:point to zhe first element of an array
* len:the length of the array
* out:
* the array
* checked error:
* pa==null;
* non checked error:
* out of the array
* exception:
* out of the array
* */
static void quanPaiLie(int *pa,int len);
/*
* func describe:
* li yong zuo xuan quan pai lie n ge yuan su
* in:
* out:
* if there is a return value
* checked error:
* pa==null;
* non checked error:
*
* exception:
* array out of range
* */
static void printIntArray(int *start,int *end);
/*
* func describe:
* how to get the start pointer of the array,that is the question.a global value?a stati
* c value? a inner value?
* in:
* start:the start pointer value of the array
* end:the end opinter+1value of the array
* */
int main(int argc,char **argv){
int array[]={1,2,3,4};
printIntArray(array,NULL);
quanPaiLie(array,sizeof(array)/sizeof(array[0]));
printIntArray(NULL,array+4);
return 0;
}
static int *leftRotate(int *pa,int len){
int tmp;
int i;
assert(pa!=NULL);
assert(len>0);
tmp=pa[0];
for (i=0;i<len-1;++i){
pa[i]=pa[i+1];
}
pa[len-1]=tmp;
return pa;
}
static void quanPaiLie(int *pa,int len){
int i;
assert(pa!=NULL);
assert(len>0);
//printIntArray(pa,NULL);
if (len==1){
//tu be insert
printIntArray(NULL,pa+1);
return ;
}
for (i=0;i<len;++i){
quanPaiLie(pa+1,len-1);
leftRotate(pa,len);
}
return ;
}
static void printIntArray(int *start,int *end){
static int *staticStart;
int *p;
if(start==NULL){
assert(staticStart);
assert(end);
for(p=staticStart;p!=end;++p){
printf("%d ",*p);
}
printf("\n");
}else if(start!=NULL){
staticStart=start;
}
}
# undef swap
* =====================================================================================
*
* Filename: basedonleftrotate.c
*
* Description:
*
* Version: 1.0
* Created: 09/03/2012 09:54:25 AM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (erliang),
* Organization:
*
* =====================================================================================
*/
/*
* n个互异元素的全排列
* 此解法摘抄至网络luxuery-zh.iteye.com/blog/1669841
* 简单描述:
* 根本思想是:n个互异元素的全排列,n个元素分别放在第一个位置剩下的n-1个元素全排列的排列的并(这里他们之间并没有交集)
* 固定第一个元素将其他元素全排列,然后左旋一位,再固定第一个元素(原第二个元素)其他元素全排列,如此往复直到所有元素都移动到了第一个元素位置一次。
* */
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#define swap(a,b) {(a)=(a)^(b);(b)=(a)^(b);(a)=(a)^(b);}
static int *leftRotate(int *pa,int len);
/*
* func describe:array left rotate one place
* in:
* pa:point to zhe first element of an array
* len:the length of the array
* out:
* the array
* checked error:
* pa==null;
* non checked error:
* out of the array
* exception:
* out of the array
* */
static void quanPaiLie(int *pa,int len);
/*
* func describe:
* li yong zuo xuan quan pai lie n ge yuan su
* in:
* out:
* if there is a return value
* checked error:
* pa==null;
* non checked error:
*
* exception:
* array out of range
* */
static void printIntArray(int *start,int *end);
/*
* func describe:
* how to get the start pointer of the array,that is the question.a global value?a stati
* c value? a inner value?
* in:
* start:the start pointer value of the array
* end:the end opinter+1value of the array
* */
int main(int argc,char **argv){
int array[]={1,2,3,4};
printIntArray(array,NULL);
quanPaiLie(array,sizeof(array)/sizeof(array[0]));
printIntArray(NULL,array+4);
return 0;
}
static int *leftRotate(int *pa,int len){
int tmp;
int i;
assert(pa!=NULL);
assert(len>0);
tmp=pa[0];
for (i=0;i<len-1;++i){
pa[i]=pa[i+1];
}
pa[len-1]=tmp;
return pa;
}
static void quanPaiLie(int *pa,int len){
int i;
assert(pa!=NULL);
assert(len>0);
//printIntArray(pa,NULL);
if (len==1){
//tu be insert
printIntArray(NULL,pa+1);
return ;
}
for (i=0;i<len;++i){
quanPaiLie(pa+1,len-1);
leftRotate(pa,len);
}
return ;
}
static void printIntArray(int *start,int *end){
static int *staticStart;
int *p;
if(start==NULL){
assert(staticStart);
assert(end);
for(p=staticStart;p!=end;++p){
printf("%d ",*p);
}
printf("\n");
}else if(start!=NULL){
staticStart=start;
}
}
# undef swap