ttp://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1005、
搞死我了。。。下面是找到的答案。。
Description
Generate the complete permutation of
Input
Each
non-negative integer N (0< N < 9)
Output
Output N! Lines, according to lexicographic order.
Sample Input
3
Sample Output
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
#include
#include
#include
using namespace std;
int n;
int ans[10];
int used[10];
void perm(int k){
{ans[k]=i+1;used[i]=1;}
perm(k+1);
ans[k]=0;
used[i]=0;
}
int main()
{
cin>>n;
perm(0);
return 0;
}
//自己还用链表,我晕 这时间和空间都大大的大
//把问题考虑复杂了 把数字借整数进行一个转换。。。。
#include
#include
#define MAXSIZE 9
struct list{
int val;
struct list *next;
};
struct list *create_next(struct list*
head,int n);
void print_list(struct list*,int
n);
void sort(struct list*);
int coef[MAXSIZE],a[MAXSIZE];
int totalNum=0;
int main()
{
int n=0;
scanf("%d",&n);
struct list *head_new = NULL;
int i=0;
coef[0]=1;
for(i=1;i
coef[i]=coef[i-1]*10;
}
for(i=1;i<=n;i++)
head_new=create_next(head_new,i);
sort(head_new);
print_list(head_new,n);
return 0;
}
struct list *create_next(struct list*
head,int n)
{
struct list *p=head;
struct list *head_new=NULL;
struct list *p_new=NULL;
if(head==NULL){
head = (struct list*)malloc(sizeof(struct
list));
head->val=1;
head->next=NULL;
return head;
}
totalNum=0;
for(;p!=NULL;p=p->next){
int temp=0;
int j=0;
for(j=0;j
if(j!=0)
a[j]=p->val-temp;
else
a[j]=p->val;
a[j]=a[j]/coef[n-2-j];
temp+=a[j]*coef[n-2-j];
}
for(j=0;j
int result=0;
int idx=0;
int k=0;
for(k=0;k
if(k==j){
result+=n*coef[n-1-k];
continue;
}
else{
result+=a[idx]*coef[n-1-k];
}
idx++;
}
struct list* temp_new = (struct
list*)malloc(sizeof(struct list));
temp_new->val=result;
temp_new->next=NULL;
totalNum++;
if(head_new==NULL){
head_new = temp_new;
p_new = temp_new;
}
else{
p_new->next=temp_new;
p_new = p_new->next;
}
}
}
return head_new;
}
void print_list(struct list* head,int
n)
{
for(;head!=NULL;head=head->next){
int temp=0;
int i=0;
for(i=0;i
int num =
(head->val-temp)/coef[n-1-i];
temp += num*coef[n-1-i];
printf("%d ",num);
}
printf("\n");
}
}
void sort(struct list* head)
{
struct list* p= head;
int i,j;
for(i=totalNum-1;i>0;i--){
for(j=0;j
if(p->val>p->next->val){
int temp = p->next->val;
p->next->val = p->val;
p->val = temp;
}
p=p->next;
}
p=head;
}
}