E voila' ho trovato tutte le soluzioni!!:
codice:package esercizi.ese_4; import java.io.PrintStream; import java.nio.Buffer; import java.util.StringTokenizer; /** * * @author mau2 */ public class TryPermutation { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int [] numbers = {1, 2, 3, 4}; int [][] INTpermutazioni; String[] STRpermutazioni; INTpermutazioni = PermutaInt.permuta( numbers ); for (int i = 0; i < INTpermutazioni.length; i++) { for (int j = 0; j < INTpermutazioni[i].length; j++) { System.out.print(INTpermutazioni[i][j] + " "); } System.out.println(); } STRpermutazioni = PermutaPhrase.permuta("il mio gatto"); //System.out.println(STRpermutazioni.length); for (int i = 0; i < STRpermutazioni.length; i++) { System.out.print(STRpermutazioni[i]); System.out.println(); } //scrittura dati su file... } private static class PermutaInt { static int [][] p; static int index = 0; public static int[][] permuta(int [] number){ p = new int[factorial(number.length)][number.length]; generaPermutazioni(number, 0); return p; } public static void generaPermutazioni(int[] B, int estremoinferiore) { int temp; // stampa permutazione attuale if (estremoinferiore == B.length-1) { for (int j = 0; j < B.length; j++){ p[index][j] = B[j]; } index++; } for (int i = estremoinferiore; i < B.length; i++) { temp = B[estremoinferiore]; B[estremoinferiore] = B[i]; B[i] = temp; generaPermutazioni(B, estremoinferiore + 1); temp = B[estremoinferiore]; B[estremoinferiore] = B[i]; B[i] = temp; } } } private static class PermutaPhrase{ static String [] strReturned; static String [][] tmpStr; static int index = 0; public static String[] permuta(String str){ StringTokenizer tok = new StringTokenizer(str); int countToken = tok.countTokens(); tmpStr = new String[factorial(countToken)][countToken]; //intanto popoliamo il primo indice dell'array bidimensionale //con la striga str passata come argomento //anche se tale popolamento verra' sovrascritto serve per passare l'array //monodimenrionale al metodo generaPermutazioni for (int i = 0; i < countToken; i++){ tmpStr[0][i] = tok.nextToken(); } generaPermutazioni(tmpStr[0], 0); //adesso popoliamo l'array monodimensionale con tutte le permutazioni StringBuffer [] buffer = new StringBuffer[factorial(tmpStr[0].length)]; strReturned = new String[factorial(tmpStr[0].length)]; for (int i = 0; i < buffer.length; i++) { buffer[i] = new StringBuffer(); } for (int index = 0, i = 0; i < tmpStr.length; i++, index++) { for (int j = 0; j < tmpStr[i].length; j++) { buffer[index].append(tmpStr[i][j] + " "); } } for (int i = 0; i < buffer.length; i++) { strReturned[i] = buffer[i].toString(); } return strReturned; } public static void generaPermutazioni(String[] B, int estremoinferiore) { String temp; // stampa permutazione attuale if (estremoinferiore == B.length-1) { for (int j = 0; j < B.length; j++){ tmpStr[index][j] = B[j]; } index++; } for (int i = estremoinferiore; i < B.length; i++) { temp = B[estremoinferiore]; B[estremoinferiore] = B[i]; B[i] = temp; generaPermutazioni(B, estremoinferiore + 1); temp = B[estremoinferiore]; B[estremoinferiore] = B[i]; B[i] = temp; } } } private static int factorial(int num){ int count = 1; for (int i = 2; i <= num; i++) { count *= i; } return count; } }

Rispondi quotando