【问题标题】:Why Is This Code Running Longer Character Combinations Than Necessary?为什么这段代码运行的字符组合比必要的要长?
【发布时间】:2019-01-20 14:54:13
【问题描述】:

我是一名数学专业的学生,​​几乎没有编程经验,但我写这个是为了表现得像一个蛮力算法。它似乎运行良好,除了它将所有密码组合运行到 3 个字符,密码短至 2。此外,我确信还有一种方法可以重构 for 和 if 语句。任何帮助将不胜感激,谢谢。

我已经在测试是否有一些 if 语句没有执行,看起来带有“console.writeln(Is this execution)”的语句没有执行,但我不太确定.

public Form1()
    {
        InitializeComponent();
    }
    static char[] Match ={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j' ,'k','l','m','n','o','p',
                    'q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','C','L','M','N','O','P',
                    'Q','R','S','T','U','V','X','Y','Z','!','?',' ','*','-','+'};
    private string[] tempPass;
    private void button1_Click(object sender, EventArgs e)
    {
        string tempPass1 = "lm";
        string result = String.Empty;
        int passLength = 1;
        int maxLength = 17;
        tempPass = new string[passLength];
        for (int i = 0; i < Match.Length; i++)
        {
            if (tempPass1 != result)
            {
                tempPass[0] = Match[i].ToString();
                result = String.Concat(tempPass);

                if (passLength > 1)
                {

                    for (int j = 0; j < Match.Length; j++)
                    {
                        if (tempPass1 != result)
                        {
                            tempPass[1] = Match[j].ToString();
                            result = String.Concat(tempPass);
                            if (passLength > 2)
                            {

                                for (int k = 0; k < Match.Length; k++)
                                {

                                    if (tempPass1 != result)
                                    {
                                        tempPass[2] = Match[k].ToString();
                                        result = String.Concat(tempPass);
                                    if (tempPass[0] == "+" && tempPass[1] == "+" && tempPass[2] == "+" && tempPass1 != result)
                                        {
                                            Console.WriteLine("This will execute?");
                                            passLength++;
                                            tempPass = new string[passLength];
                                            k = 0;
                                            j = 0;
                                            i = 0;
                                        }
                                        else if (result == tempPass1)
                                        {
                                            Console.WriteLine("Broken");
                                            Console.WriteLine("This is big gay: " + result);
                                            break;
                                        }


                                    }

                                }
                            }
                            if (tempPass[0] == "+" && tempPass[1] == "+" && tempPass1 != result)
                            {
                                Console.WriteLine("Did this execute?");
                                passLength++;
                                tempPass = new string[passLength];
                                j = 0;
                                i = 0;
                            }
                            else if (result == tempPass1)
                            {
                                Console.WriteLine("Broken");
                                Console.WriteLine("This is bigger gay: " + result);
                                break;
                            }

                        }

                    }
                }
                //tempPass[1] = "World!";
                //Console.WriteLine(result);
                if (tempPass[tempPass.Length - 1] == "+" && tempPass1 != result)
                {
                    passLength++;
                    tempPass = new string[passLength];
                    Console.WriteLine(tempPass.Length + "  " + result + "  " + "Success");
                    Console.WriteLine(i);
                    i = 0; /**update
                    j = 0;
                    k = 0;
                    l = 0;
                    m = 0;*/
                }
                else if (result == tempPass1)
                {
                    Console.WriteLine("Broken");
                    Console.WriteLine("This is biggest gay: " + result);

                }
            }
        }
    }

【问题讨论】:

  • 这是开始使用调试器的好时机。使用调试器,您可以在要暂停代码执行的任何位置放置断点,然后您可以在代码执行时逐行单步执行代码,观察确切的运行时行为和每行变量的变化值。当你这样做时,你的程序首先在哪一行产生了意想不到的结果?在该行执行之前变量的值是多少?观察到的结果是什么?预期的结果是什么?为什么?
  • 我已经完成了它,它在 for (int k = 0;) 行中产生了意想不到的结果。列表长度之前的值是 2,之后是 3。我希望它甚至不会执行那行代码。
  • @Christheyankee:是时候继续调试了。您希望变量等于 2,但它等于 3?为什么你期望它等于 2?有有限数量的行可以修改变量。当您单步调试调试器时,观察这些行何时执行。目前,您真正想说的是您不希望执行 if 块,但确实如此。答案是if 条件显然是true。当您提出更具体的问题时,我们当然可以提供帮助,但目前您只是要求我们为您调试程序。这不是我们做的事情。
  • 我强烈推荐 Adrian Akison 写的 Combinatorics Libary

标签: c# winforms


【解决方案1】:

玩这个;根据我的回答 here 修改。它将向您展示所有 2 和 3 长度组合。单击该按钮将开始/停止生成过程。你需要一个按钮、标签和一个计时器:

public partial class Form1 : Form
{

    private Revision rev;

    public Form1()
    {
        InitializeComponent();
        rev = new Revision("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!? *-+", "00");
        label1.Text = rev.CurrentRevision;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Enabled = !timer1.Enabled;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        rev.NextRevision();
        if (rev.CurrentRevision.Length == 4)
        {
            timer1.Stop();
            MessageBox.Show("Sequence Complete");

            // make it start back at the beginning?
            rev = new Revision("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!? *-+", "00");
            label1.Text = rev.CurrentRevision;
        }
        else
        {
            label1.Text = rev.CurrentRevision;
        }
    }
}

public class Revision
{

    private string chars;
    private char[] values;

    private System.Text.StringBuilder curRevision;

    public Revision()
    {
        this.DefaultRevision();
    }

    public Revision(string validChars)
    {
        if (validChars.Length > 0)
        {
            chars = validChars;
            values = validChars.ToCharArray();
            curRevision = new System.Text.StringBuilder(values[0]);
        }
        else
        {
            this.DefaultRevision();
        }
    }

    public Revision(string validChars, string startingRevision)
        : this(validChars)
    {
        curRevision = new System.Text.StringBuilder(startingRevision.ToUpper());
        int i = 0;
        for (i = 0; i <= curRevision.Length - 1; i++)
        {
            if (Array.IndexOf(values, curRevision[i]) == -1)
            {
                curRevision = new System.Text.StringBuilder(values[0]);
                break;
            }
        }
    }

    private void DefaultRevision()
    {
        chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        values = chars.ToCharArray();
        curRevision = new System.Text.StringBuilder(values[0]);
    }

    public string ValidChars
    {
        get { return chars; }
    }

    public string CurrentRevision
    {
        get { return curRevision.ToString(); }
    }

    public string NextRevision(int numRevisions = 1)
    {
        bool forward = (numRevisions > 0);
        numRevisions = Math.Abs(numRevisions);
        int i = 0;
        for (i = 1; i <= numRevisions; i++)
        {
            if (forward)
            {
                this.Increment();
            }
            else
            {
                this.Decrement();
            }
        }
        return this.CurrentRevision;
    }

    private void Increment()
    {
        char curChar = curRevision[curRevision.Length - 1];
        int index = Array.IndexOf(values, curChar);
        if (index < (chars.Length - 1))
        {
            index = index + 1;
            curRevision[curRevision.Length - 1] = values[index];
        }
        else
        {
            curRevision[curRevision.Length - 1] = values[0];
            int i = 0;
            int startPosition = curRevision.Length - 2;
            for (i = startPosition; i >= 0; i += -1)
            {
                curChar = curRevision[i];
                index = Array.IndexOf(values, curChar);
                if (index < (values.Length - 1))
                {
                    index = index + 1;
                    curRevision[i] = values[index];
                    return;
                }
                else
                {
                    curRevision[i] = values[0];
                }
            }
            curRevision.Insert(0, values[0]);
        }
    }

    private void Decrement()
    {
        char curChar = curRevision[curRevision.Length - 1];
        int index = Array.IndexOf(values, curChar);
        if (index > 0)
        {
            index = index - 1;
            curRevision[curRevision.Length - 1] = values[index];
        }
        else
        {
            curRevision[curRevision.Length - 1] = values[values.Length - 1];
            int i = 0;
            int startPosition = curRevision.Length - 2;
            for (i = startPosition; i >= 0; i += -1)
            {
                curChar = curRevision[i];
                index = Array.IndexOf(values, curChar);
                if (index > 0)
                {
                    index = index - 1;
                    curRevision[i] = values[index];
                    return;
                }
                else
                {
                    curRevision[i] = values[values.Length - 1];
                }
            }
            curRevision.Remove(0, 1);
            if (curRevision.Length == 0)
            {
                curRevision.Insert(0, values[0]);
            }
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多