تحية طيبة
باللغة البرمجية التي تتقنها، أُكتب برنامج يطلب من المستخدم أدخال أرقام وأحرف لإيحاد أكبر عدد من كلمات السر!
=======
مثال:
المستخدم: 123
المفروض من البرنامج أن يطبع:
123
132
213
231
312
321
=======
رابط للتجربة :
الكود:
private static int shiftCount = 1;
public static void main (String[] args) throws java.lang.Exception
{
printPasswords("123");
}
private static void printPasswords(String password)
{
if(password.length() > 0)
{
long possibilities = factorial(password.length());
for (long i = 0 ; i < possibilities ; i++)
{
password = shift(password);
System.out.println(password);
}
}
}
private static String shift(String text)
{
if (shiftCount == text.length()) shiftCount = 1;
String targetChar = text.substring(shiftCount - 1, shiftCount);
String restOfText = text.replace(targetChar, "");
StringBuilder finalResult = new StringBuilder(restOfText);
finalResult.insert(shiftCount , targetChar);
shiftCount++;
return finalResult.toString();
}
private static long factorial(int n)
{
long result = 1;
for (int i = 1 ; i <= n ; i++)
{
result *= i;
}
return result;
}
التعليقات