var I: Integer; S, Inicial, T: String; Fichero: Text; NumSoluciones: Longint; function SiguientePermutacion(S: String): String; var I, J: Byte; T: String; begin I := Length(S); T := S[I]; Dec(I); while (I > 0) and (S[I] >= S[I+1]) do begin T := T + S[I]; Dec(I); end; if I = 0 then SiguientePermutacion := '' else begin J := 1; while (J <= Length(T)) and (T[J] <= S[I]) do Inc(J); SiguientePermutacion := Copy(S,1,I-1) + T[J] + Copy(T,1,J-1) + S[I] + Copy(T,J+1,255); end; end; function TodosDistintos(S: String): Boolean; var I, J: Byte; begin TodosDistintos := False; for I := 1 to Length(S)-1 do for J := I+1 to Length(S) do if S[I] = S[J] then Exit; TodosDistintos := True; end; begin Assign(Fichero, 'salida.txt'); Rewrite(Fichero); NumSoluciones := 0; S := '123456789'; T := S; Writeln('Calculando ...'); repeat for I := 1 to Length(S) do T[I] := Chr(48+Abs(Ord(S[I])-48-I)); if TodosDistintos(T) then begin Writeln(S); Inc(NumSoluciones); Write(Fichero, S); if NumSoluciones mod 6 = 0 then Writeln(Fichero) else Write(Fichero, '|'); end; S := SiguientePermutacion(S); until S= ''; Writeln(NumSoluciones, ' soluciones.'); Close(Fichero); end.