Advertisement
Guest User

Untitled

a guest
Oct 29th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.88 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package scalaapplication4
  8.  
  9. object Main {
  10.   val pi=Array(1,2,3,4,5)
  11.   var x=0;
  12.   def main(args: Array[String]): Unit = {      
  13.     perm(pi.length)
  14.     println(x)
  15.   }
  16.  
  17.   def perm(m:Int):Unit={    
  18.     if (m==1){
  19.       println(pi.deep.mkString(""))
  20.     }
  21.     else{
  22.       for (i<-1 to m){
  23.         perm(m-1);
  24.         if (i<m){
  25.           var tmp = pi(B(m, i)-1);
  26.           pi(B(m, i)-1)=pi(m-1);
  27.           pi(m-1)=tmp;
  28.         }          
  29.       }
  30.     }
  31.     x+=1;  /*Counting permutations*/
  32.   }
  33.  
  34.   def B(m:Int, i:Int):Int={
  35.     if(m%2==0 && m>2){
  36.       if(i<m-1){
  37.         return i;
  38.       }
  39.       else{
  40.         return m-2;
  41.       }
  42.     }
  43.     else{
  44.       return m-1;
  45.     }
  46.   }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement