Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package scalaapplication4
- object Main {
- val pi=Array(1,2,3,4,5)
- var x=0;
- def main(args: Array[String]): Unit = {
- perm(pi.length)
- println(x)
- }
- def perm(m:Int):Unit={
- if (m==1){
- println(pi.deep.mkString(""))
- }
- else{
- for (i<-1 to m){
- perm(m-1);
- if (i<m){
- var tmp = pi(B(m, i)-1);
- pi(B(m, i)-1)=pi(m-1);
- pi(m-1)=tmp;
- }
- }
- }
- x+=1; /*Counting permutations*/
- }
- def B(m:Int, i:Int):Int={
- if(m%2==0 && m>2){
- if(i<m-1){
- return i;
- }
- else{
- return m-2;
- }
- }
- else{
- return m-1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement