Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.85 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. package zadanie1;
  7.  
  8. //import java.lang.reflect.Array;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. import java.util.Random;
  13. import java.util.Scanner;
  14.  
  15. /**
  16.  *
  17.  * @author Matej
  18.  */
  19.  
  20.  import java.io.*;
  21. import java.awt.GraphicsEnvironment;
  22. import java.net.URISyntaxException;
  23.  
  24.  
  25.  
  26. public class Zadanie1 {
  27.  
  28.     private static int count;
  29.     private static char[] TSA={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  30.     private static String vyslednaPermutacia="";
  31.     private static List<String> listOfPermutations = new ArrayList<String>();
  32.     private static List<Integer> pouzite = new ArrayList<Integer>();
  33.    
  34.  
  35.     /**
  36.      * @param args the command line arguments
  37.      */
  38.    
  39.    
  40.     public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
  41.         // TODO code application logic here
  42.        
  43.        /* Console console = System.console();
  44.         if(console == null && !GraphicsEnvironment.isHeadless()){
  45.             //String filename = Main.class.getProtectionDomain().getCodeSource().getLocation().toString().substring(6);
  46.             Runtime.getRuntime().exec(new String[]{"cmd","/c","start","cmd","/k","java -jar \"" + "Zadanie1.jar" + "\""});
  47.         }else{*/
  48.            
  49.        
  50.         count=0;
  51.         System.out.println("Zadajte velkost mnoziny: ");
  52.         Scanner scanner2 = new Scanner(System.in);
  53.         int n = scanner2.nextInt();
  54.         generateString(n,TSA);
  55.         //vyslednaPermutacia="ABCDE";
  56.         char[] pomocnePole;
  57.         pomocnePole=vyslednaPermutacia.toCharArray();
  58.         Arrays.sort(pomocnePole);
  59.         vyslednaPermutacia=new String(pomocnePole);
  60.         permutation(vyslednaPermutacia);
  61.         System.out.println("Prva permutacia je: ");
  62.         System.out.println(vyslednaPermutacia);
  63.         System.out.println("Napiste mnozinu, ktorej poradove cislo hladate (velkymi pismenami): ");
  64.         Scanner scanner = new Scanner(System.in);
  65.         String p = scanner.nextLine();
  66.         System.out.println("Poradove cislo mnoziny je: ");
  67.         System.out.println(vratK(p));
  68.         System.out.println("Zadajte poradove cislo mnoziny, ktoru hladate: ");
  69.         Scanner scanner3 = new Scanner(System.in);
  70.         int n2 = scanner3.nextInt();
  71.         System.out.println("Hladana permutacia je: ");
  72.         System.out.println(vratPermutaciu(n2));
  73.         System.out.println("***KONIEC PROGRAMU***");
  74.                
  75.     }
  76.    
  77.     public static void permutation(String str) {
  78.         permutation("", str);
  79.     }
  80.    
  81.    
  82.    
  83.     public static void generateString(int n, char[] TSA){
  84.         Random nahodnyGenerator = new Random();
  85.         //System.out.println(TSA);
  86.         int nahodnyIndex;
  87.         for(int i=0;i<n;i++){  
  88.             nahodnyIndex=nahodnyGenerator.nextInt(TSA.length);
  89.             if(vyslednaPermutacia.indexOf(TSA[nahodnyIndex])>=0)
  90.                 i--;
  91.             else
  92.                 vyslednaPermutacia=vyslednaPermutacia+TSA[nahodnyIndex];
  93.                
  94.         }
  95.        
  96.     }
  97.  
  98.     private static void permutation(String prefix, String str) {
  99.        int n = str.length();
  100.       if (n == 0){
  101.            //System.out.println(prefix);
  102.            listOfPermutations.add(prefix);
  103.            count++;
  104.        }
  105.    
  106.         else {
  107.             for (int i = 0; i < n; i++)
  108.                  permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
  109.          }
  110.     }
  111.    
  112.     public static int factorial(int number) {
  113.       if (number <= 1)
  114.          return 1;
  115.       else
  116.          return number * factorial(number - 1);
  117.    }
  118.    
  119.     private static int vratK(String permutacia){
  120.         char[] hladanaPermutacia = permutacia.toCharArray();
  121.         char[] prvaPermutacia = vyslednaPermutacia.toCharArray();
  122.         int[] ocislovanaPermutacia = new int[permutacia.length()];
  123.         int poradie=0;
  124.         int k=0;
  125.      
  126.         for(int i=0; i<permutacia.length();i++){
  127.             for(int j=0;j<permutacia.length();j++){
  128.                 if(hladanaPermutacia[i]==prvaPermutacia[j]){
  129.                     poradie=j+1;
  130.                     ocislovanaPermutacia[k++]=poradie;
  131.                     //System.out.println(poradie);
  132.                     //System.out.println(ocislovanaPermutacia[k]);
  133.                     //System.out.println(", ");
  134.                 }
  135.             }
  136.         }
  137.        
  138.         int[] pomocnePoleTi = new int[permutacia.length()];
  139.         List<Integer> pouzite = new ArrayList<Integer>();
  140.         for(int i=0;i<permutacia.length();i++){
  141.             pouzite.add(i+1);
  142.         }
  143.        
  144.         for(int i=0; i<permutacia.length();i++){
  145.             pomocnePoleTi[i]=pouzite.indexOf(ocislovanaPermutacia[i])+1;
  146.             pouzite.remove(pouzite.indexOf(ocislovanaPermutacia[i]));
  147.             //System.out.println(pomocnePoleTi[i]);
  148.         }
  149.        
  150.         int sum = permutacia.length()-1;
  151.         int l=0;
  152.         int vysledneK=0;
  153.        
  154.         while(sum>=0){
  155.             vysledneK += (pomocnePoleTi[l]-1)*factorial(sum);
  156.             sum--;
  157.             l++;
  158.         }
  159.        
  160.         vysledneK+=1;
  161.          
  162.         //System.out.println(vysledneK);
  163.         return vysledneK;
  164.        
  165.     }
  166.    
  167.        
  168.     private static String vratPermutaciu(int k){
  169.        
  170.         List<Integer> pomocnePoleTi = new ArrayList<Integer>();
  171.         k--;
  172.         int velkostPermutacie = vyslednaPermutacia.length()-1;
  173.         int hlp1 = velkostPermutacie;
  174.         int hlp2 = 1;
  175.         int hlp3 = 1;
  176.        
  177.        
  178.         while(hlp1>=0){
  179.             pouzite.add(hlp2);
  180.             hlp2++;
  181.             hlp1--;
  182.         }
  183.         int velkostPola = pouzite.size();
  184.        
  185.         for (int i = velkostPermutacie; i > 0; i--) {
  186.             hlp3=1;
  187.             if ((k>=factorial(i)) && (k>0) ) {
  188.                 while((k - hlp3*factorial(i)>=0 )){
  189.                     hlp3++;
  190.                 }            
  191.             k -= (hlp3-1)*factorial(i);
  192.             pomocnePoleTi.add(hlp3);
  193.             }
  194.             else {
  195.                 pomocnePoleTi.add(1);
  196.             }
  197.         }
  198.            
  199.         int[] vyslednePole = new int [velkostPola];
  200.                
  201.         for (int i = 0; i < velkostPola-1; i++) {          
  202.             vyslednePole[i] = pouzite.remove(pomocnePoleTi.get(i)-1);
  203.         }
  204.         vyslednePole[velkostPola-1] = pouzite.remove(0) ;
  205.                
  206.         String vyslednyRetazec = "";
  207.         for (int i = 0; i < velkostPermutacie+1; i++) {          
  208.             vyslednyRetazec += vyslednaPermutacia.charAt(vyslednePole[i] -1);
  209.         }
  210.         return vyslednyRetazec;
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement