DOMANDA Creare combinazioni numeriche

Joker98

Utente Attivo
147
5
CPU
i5 2500k 3.3 @ 4.5 ghz
Scheda Madre
p8z68 v lx
RAM
G.skill ripjawsx12800 1600 mhz CL8 4gb x2
GPU
xfx hd 6950 xxx
PSU
corsair hx 750w
Case
cm 690 nvidia edition
OS
win 7 ultimate
Salve a tutti! :)
Avrei bisogno di creare un programmino php che mi faccia tutte le combinazioni dei numeri 0 1 2 3 4 5 (in tot 243) .
Mi serve per la risoluzione d uno stupido gioco.
Non ho bisogno d salvare le query nel database.
Sono solo riuscito con le mie poche conoscenze a fare uno script che crea numeri casuali con quelle cifre.
Grazie in anticipo ! :D
 

Roberto Buonanno

Amministratore
1,721
257
Ciao, ti serve un generatore di permutazioni! Ne ho trovato uno, eccolo:
PHP:
Permutation Generator

<? 
function permutations($letters,$num){ 
    $last = str_repeat($letters{0},$num); 
    $result = array(); 
    while($last != str_repeat(lastchar($letters),$num)){ 
        $result[] = $last; 
        $last = char_add($letters,$last,$num-1); 
    } 
    $result[] = $last; 
    return $result; 
} 
function char_add($digits,$string,$char){ 
    if($string{$char} <> lastchar($digits)){ 
        $string{$char} = $digits{strpos($digits,$string{$char})+1}; 
        return $string; 
    }else{ 
        $string = changeall($string,$digits{0},$char); 
        return char_add($digits,$string,$char-1); 
    } 
} 
function lastchar($string){ 
    return $string{strlen($string)-1}; 
} 
function changeall($string,$char,$start = 0,$end = 0){ 
    if($end == 0) $end = strlen($string)-1; 
    for($i=$start;$i<=$end;$i++){ 
        $string{$i} = $char; 
    } 
    return $string; 
} 
?> 

To use this Generator you can do something like this : 

<? 
$Array=permutations("ABC",3); 
for($i=0 ; $i < count($Array) ; $i++) { 
        echo "$i." . $Array[$i] . "<BR>"; 
} 
?> 

The output would look like this : 

0.AAA 
1.AAB 
2.AAC 
3.ABA 
4.ABB 
5.ABC 
6.ACA 
7.ACB 
8.ACC 
9.BAA 
10.BAB 
11.BAC 
12.BBA 
13.BBB 
14.BBC 
15.BCA 
16.BCB 
17.BCC 
18.CAA 
19.CAB 
20.CAC 
21.CBA 
22.CBB 
23.CBC 
24.CCA 
25.CCB 
26.CCC

Preso da qui: Php sample code - Permutation Generator - Source code examples
 

Joker98

Utente Attivo
147
5
CPU
i5 2500k 3.3 @ 4.5 ghz
Scheda Madre
p8z68 v lx
RAM
G.skill ripjawsx12800 1600 mhz CL8 4gb x2
GPU
xfx hd 6950 xxx
PSU
corsair hx 750w
Case
cm 690 nvidia edition
OS
win 7 ultimate
Non riesco a farlo funzionare ... sono ignorante in materia :muro: Come lo devo applicare ?
p.s poi se devo limitare il risultato a tre cifre basta che cambio solo "ABC" con "012345" e lascio il 3?

Codice:
[LEFT][COLOR=#0000CC][FONT=monospace]<?  
$Array[/FONT][/COLOR][COLOR=#006600][FONT=monospace]=[/FONT][/COLOR][COLOR=#0000CC][FONT=monospace]permutations[/FONT][/COLOR][COLOR=#006600][FONT=monospace]([/FONT][/COLOR][COLOR=#CC0000][FONT=monospace]"ABC"[/FONT][/COLOR][COLOR=#006600][FONT=monospace],[/FONT][/COLOR][COLOR=#0000CC][FONT=monospace]3[/FONT][/COLOR][COLOR=#006600][FONT=monospace]);  
for([/FONT][/COLOR][COLOR=#0000CC][FONT=monospace]$i[/FONT][/COLOR][COLOR=#006600][FONT=monospace]=[/FONT][/COLOR][COLOR=#0000CC][FONT=monospace]0 [/FONT][/COLOR][COLOR=#006600][FONT=monospace]; [/FONT][/COLOR][COLOR=#0000CC][FONT=monospace]$i [/FONT][/COLOR][COLOR=#006600][FONT=monospace]< [/FONT][/COLOR][COLOR=#0000CC][FONT=monospace]count[/FONT][/COLOR][COLOR=#006600][FONT=monospace]([/FONT][/COLOR][COLOR=#0000CC][FONT=monospace]$Array[/FONT][/COLOR][COLOR=#006600][FONT=monospace]) ; [/FONT][/COLOR][COLOR=#0000CC][FONT=monospace]$i[/FONT][/COLOR][COLOR=#006600][FONT=monospace]++) {  
        echo [/FONT][/COLOR][COLOR=#CC0000][FONT=monospace]"$i." [/FONT][/COLOR][COLOR=#006600][FONT=monospace]. [/FONT][/COLOR][COLOR=#0000CC][FONT=monospace]$Array[/FONT][/COLOR][COLOR=#006600][FONT=monospace][[/FONT][/COLOR][COLOR=#0000CC][FONT=monospace]$i[/FONT][/COLOR][COLOR=#006600][FONT=monospace]] . [/FONT][/COLOR][COLOR=#CC0000][FONT=monospace]"<BR>"[/FONT][/COLOR][COLOR=#006600][FONT=monospace];  
}  
[/FONT][/COLOR][COLOR=#0000CC][FONT=monospace]?>[/FONT][/COLOR][COLOR=#000000][FONT=monospace] [/FONT][/COLOR][/LEFT]
 
Ultima modifica:

Joker98

Utente Attivo
147
5
CPU
i5 2500k 3.3 @ 4.5 ghz
Scheda Madre
p8z68 v lx
RAM
G.skill ripjawsx12800 1600 mhz CL8 4gb x2
GPU
xfx hd 6950 xxx
PSU
corsair hx 750w
Case
cm 690 nvidia edition
OS
win 7 ultimate

Joker98

Utente Attivo
147
5
CPU
i5 2500k 3.3 @ 4.5 ghz
Scheda Madre
p8z68 v lx
RAM
G.skill ripjawsx12800 1600 mhz CL8 4gb x2
GPU
xfx hd 6950 xxx
PSU
corsair hx 750w
Case
cm 690 nvidia edition
OS
win 7 ultimate
Fatto dopo un po di prove funge ! GRZ mille ! :love: Avevo fatto un errore io nei form dove inserire le cifre, me culpa!
 
  • Mi piace
Reazioni: Roberto Buonanno

Entra

oppure Accedi utilizzando
Discord Ufficiale Entra ora!

Discussioni Simili