Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void f(int *cands, int *tmp, int life, int i, int size){
- if(life==0){
- for(int i=0;i<size;i++){
- printf("%d", tmp[i]);
- putchar(i==size-1 ? '\n' : ',');
- }
- }
- else {
- int tmp_val = -1;
- for(int j=0; j<size; j++){
- if(cands[j] < 0)
- continue;
- tmp[i] = cands[j];
- tmp_val = cands[j];
- cands[j] = -1;
- f(cands, tmp, life-1, i+1, size);
- cands[j] = tmp_val;
- }
- }
- }
- void wrap_f(int n){
- int* tmp = (int *)malloc(sizeof(int)*n);
- int* cands = (int *)malloc(sizeof(int)*n);
- for(int i=0; i<n; i++)
- cands[i] = i+1;
- f(cands,tmp, n, 0, n);
- free(tmp);
- free(cands);
- }
- int main(int argc, char *argv[]){
- if(argc < 2){
- fprintf(stderr, "_ <n>\n");
- exit(-1);
- } else
- wrap_f(atoi(argv[1]));
- }
Add Comment
Please, Sign In to add comment