fork(1) download
  1. #include <stdio.h>
  2. int lcm ( int a, int b ){
  3. int x=a;
  4. int y=b;
  5. int c;
  6. while ( a != 0 ) {
  7. c = a; a = b%a; b = c;
  8. }
  9. return (x*y)/b;
  10.  
  11. }
  12.  
  13. int sgarden(int n,int a[]){
  14. int i=1;
  15. int ops=a[n];
  16. while(ops!=n){
  17. ops=a[ops];
  18. i++;
  19. }//while
  20. return i;
  21. }
  22.  
  23. int main()
  24. {
  25. int i,maxp=1;
  26. int a[]={2,3,1,5,4};
  27. for(i=0;i<(sizeof(a)/sizeof(int));i++){a[i]--;}
  28. for(i=0;i<(sizeof(a)/sizeof(int));i++){
  29. int count = sgarden(i,a);
  30. maxp=lcm(count,maxp);
  31.  
  32. }//for
  33. printf("Max permutations %d",maxp);
  34. return 0;
  35. }
  36.  
  37.  
  38.  
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
Max permutations 6