Guest User

Untitled

a guest
Jun 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void f(int *cands, int *tmp, int life, int i, int size){
  5. if(life==0){
  6. for(int i=0;i<size;i++){
  7. printf("%d", tmp[i]);
  8. putchar(i==size-1 ? '\n' : ',');
  9. }
  10. }
  11. else {
  12. int tmp_val = -1;
  13. for(int j=0; j<size; j++){
  14. if(cands[j] < 0)
  15. continue;
  16.  
  17. tmp[i] = cands[j];
  18. tmp_val = cands[j];
  19. cands[j] = -1;
  20. f(cands, tmp, life-1, i+1, size);
  21. cands[j] = tmp_val;
  22. }
  23. }
  24. }
  25.  
  26. void wrap_f(int n){
  27. int* tmp = (int *)malloc(sizeof(int)*n);
  28. int* cands = (int *)malloc(sizeof(int)*n);
  29. for(int i=0; i<n; i++)
  30. cands[i] = i+1;
  31.  
  32. f(cands,tmp, n, 0, n);
  33. free(tmp);
  34. free(cands);
  35. }
  36.  
  37. int main(int argc, char *argv[]){
  38. if(argc < 2){
  39. fprintf(stderr, "_ <n>\n");
  40. exit(-1);
  41. } else
  42. wrap_f(atoi(argv[1]));
  43. }
Add Comment
Please, Sign In to add comment