What's a good way for figuring out all possible words of a given length

后端 未结 12 1982
一生所求
一生所求 2021-02-04 17:27

I\'m trying to create an algorithm in C# which produces the following output strings:

AAAA
AAAB
AAAC
...and so on...
ZZZX
ZZZY
ZZZZ

What is the

12条回答
  •  萌比男神i
    2021-02-04 17:51

    This is an recursive version of the same functions in C#:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    
    namespace ConsoleApplication1Test
    {
        class Program
        {
            static char[] my_func( char[] my_chars, int level)
            {
                if (level > 1)
                    my_func(my_chars, level - 1);
                my_chars[(my_chars.Length - level)]++;
                if (my_chars[(my_chars.Length - level)] == ('Z' + 1))
                {
                    my_chars[(my_chars.Length - level)] = 'A';
                    return my_chars;
                }
                else
                {
                    Console.Out.WriteLine(my_chars);
                    return my_func(my_chars, level);
                }
            }
            static void Main(string[] args)
            {
                char[] text = { 'A', 'A', 'A', 'A' };
                my_func(text,text.Length);
                Console.ReadKey();
            }
        }
    }
    

    Prints out from AAAA to ZZZZ

提交回复
热议问题