C# 循环浏览随机字母,直到拼写单词

本文关键字:单词 循环 浏览 随机 | 更新日期: 2023-09-27 18:34:35

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

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

asdfjnb (初始字符串(索夫内克茨德克吉克teiuhft(随机生成字符串,生成后保留正确的字母(特斯涅格泰斯特达见证测试(最终字符串(

C# 循环浏览随机字母,直到拼写单词

另一个例子...

    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

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

将字符串视为一个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 。您选择的边界将确定达到目标所需的平均时间,以及要从中选择的字符集。