这个程序应该能猜出 7 个字符的密码吗?

Should this program work to guess a 7-character password?

我创建了这个可以猜测 7 个字符的密码的程序。当我 运行 它时,什么都没有发生。这个程序正确吗?如果有问题,请告诉我要更改的内容。这是代码:

import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
    Scanner inputReal = new Scanner(System.in);
    System.out.println("Enter real password (7 characters):");
    String realpassword = inputReal.nextLine();
    String finalpassword = "";
    char real1 = realpassword.charAt(0);
    char real2 = realpassword.charAt(1);
    char real3 = realpassword.charAt(2);
    char real4 = realpassword.charAt(3);
    char real5 = realpassword.charAt(4);
    char real6 = realpassword.charAt(5);
    char real7 = realpassword.charAt(6);

    CharSequence avaliableChars = "1234567890qwertyuiopasdfghjklzxcvbnm!@#$%^&*()?";





    char guess1;
    char guess2;
    char guess3;
    char guess4;
    char guess5;
    char guess6;
    char guess7;
    boolean notFound = true;
   while(notFound) { 

   for(int i1 = 0; i1<47 && notFound == true; i1++) {
       guess1 = avaliableChars.charAt(i1);
       for(int i2 = 0; i2<47 && notFound == true; i2++) {
           guess2 = avaliableChars.charAt(i2);
           for(int i3 = 0; i3<47 && notFound == true; i3++) {
               guess3 = avaliableChars.charAt(i3);
               for(int i4 = 0; i4<47 && notFound == true; i4++) {
                   guess4 = avaliableChars.charAt(i4);
                   for(int i5 = 0; i5<47 && notFound == true; i5++) {
                       guess5 = avaliableChars.charAt(i5);
                       for(int i6 = 0; i6<47 && notFound == true; i6++) {
                           guess6 = avaliableChars.charAt(i6);
                           for(int i7 = 0; i7<47 && notFound == true; i7++) {
                               guess7 = avaliableChars.charAt(i7);
                               String guessedpassword = "" + guess1 + guess2 + guess3 + guess4 + guess5 + guess6 + guess7;
                               if(guessedpassword.equals(realpassword)) {
                                  finalpassword = guessedpassword;
                                  notFound = false;
                               }
                           }
                       }
                   }
               }
           }
       }
    }
}
System.out.println("Guessed password:" + finalpassword);    
}

我运行它,只等了大约3-5分钟。输入真实密码后没有任何反应。

是的,这个程序可以运行,我只是用一个简单的密码“1111115”对其进行了测试。

问题只是蛮力的时机太疯狂了。时间复杂度为 O(n^7),给定任何不以数字开头的密码,如 "a111111",计算机至少执行 1.07x10^10 次操作,即超过 100 亿次操作,这将需要相当长的时间。

不过为了保证,您的算法有效。

奖励:试验您的计算机处理事情的速度。这个 500 亿次循环的程序在我的电脑上实际上运行了大约 14 秒,所以请修改它,看看如何让你的原始程序更快!

public class fast {
public static void main(String[] args) {
    long startTime = System.currentTimeMillis();
    long x = 1000000000;
    String s = "abc";
    boolean bool = true;
    for (int j = 0; j < 50; ++j) {
        for (int i = 0; i < x; ++i) {
            char a = s.charAt(2);
            // try different things, like using arrays instead of strings, etc.
        }
    }

    long endTime = System.currentTimeMillis();
    System.out.println("took " + (endTime-startTime) + " ms);
}
}