C#循环通过随机字母,直到单词拼写为止

时间:2014-12-17 03:37:40

标签: c# console

好的,我想知道如何在C#控制台应用程序中执行此操作。

基本上,我希望能够生成一个带有随机字母的字符串,在循环中我希望字符串的每个字符都被另一个随机字符替换,直到单词拼写为止。例如:

asdfjnb(初始字符串) soifnec tsdkjyk teiuhft(生成随机字符串,一旦生成就保持正确的字母) tesgneg testdha testifd 测试(最终字符串)

2 个答案:

答案 0 :(得分:1)

另一个例子......

    static void Main(string[] args)
    {
        Random r = new Random();
        string letters = "abcdefghijklmnopqrstuvwxyz";
        List<string> dictionary = new List<string>(new string[] { 
            "compartmentalization", "inheritance", "polymorphism", 
            "paradigm", "abstraction", "aggregration", "cryptography",
            "pseudocode", "recursion", "backtracking", "alogrithm"
        });

        string word = dictionary[r.Next(dictionary.Count)];
        List<int> indexes = new List<int>();

        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < word.Length; i++)
        {
            sb.Append(letters[r.Next(letters.Length)]);
            if (sb[i] != word[i])
            {
                indexes.Add(i);
            }
        }
        Console.WriteLine(sb.ToString());

        while(indexes.Count > 0)
        {
            int index;
            for(int i = indexes.Count - 1; i >= 0; i--)
            {
                index = indexes[i];
                sb[index] = letters[r.Next(letters.Length)];
                if (sb[index] == word[index])
                {
                    indexes.RemoveAt(i);
                }
            }
            Console.WriteLine(sb.ToString());
        }

        Console.ReadLine();
    }

示例输出:

guuawfixphg
gymciirrihf
skauniqldvs
mdnefisivml
zgreeisbznk
lxzeciaewoy
nuueoixpdup
fvlejioddyi
dlaeniwlsbv
iytehitguio
irxehitlpvj
ismezitvwii
iyeesitvdxe
imfekitmope
iqjevitfcse
iukepitzcae
iqdefitntue
ipieqitpcde
ihuekituble
igzelitbade
iqmejitlbce
ixbetitifce
ivkekitkkce
itcexitdhce
iqwehitjpce
isjelitsice
iccehitujce
ikzepituece
ijeekitwace
ithewitjzce
imhewitoyce
inheeitrnce
inheiitwnce
inhewitmnce
inheiitjnce
inhepitonce
inhehitdnce
inherithnce
inheritmnce
inheritnnce
inheritxnce
inheritrnce
inherittnce
inheritsnce
inheritznce
inheritdnce
inheritmnce
inheritqnce
inheritynce
inheritvnce
inheritence
inheritqnce
inheritunce
inheritunce
inheritynce
inheritunce
inheritpnce
inheritlnce
inheritznce
inherithnce
inherittnce
inheritqnce
inheritxnce
inheritence
inheritmnce
inheritcnce
inheritpnce
inheritunce
inheritvnce
inheritcnce
inheritonce
inheritpnce
inheritgnce
inheritknce
inheritqnce
inheritfnce
inherittnce
inheritunce
inheritsnce
inheritance

答案 1 :(得分:0)

在开始此过程之前,我假设问题有一个特定的目标字符串。

将字符串视为char的数组,或将其显式转换为该字符串。每次迭代都应检查字符串中的每个字符与其“目标”字符。如果当前char ==目标字符,则不要为其选择新值。

// These specified somewhere
char[] goalstring;
char[] currentstring;

// Randomized correct logic
bool finished = false;
while (!finished)
{
    finished = true;
    for (int i = 0; i < goalstring.length; ++i)
    {
         if (currentstring[i] != goalstring[i])
         {
              finished = false;
              // this function returns a random char according to your design
              currentstring[i] = GetRandomLetter(); 
         }
    }
    // render text to screen
    PrintCurrentString(currentstring);
}

获取随机字符非常简单,您可以使用Random类获取具有指定范围的int,并将其转换为char。您选择的边界将决定达到目标所需的平均时间以及可供选择的字符集。