【问题标题】:.NET C# Logic Function, recurrent function.NET C# 逻辑函数,循环函数
【发布时间】:2011-05-28 12:34:56
【问题描述】:

我有一些类似格式的字符串

XXXX-XXXX-X_X_

所有的“_”都应该用字母和数字代替,这样会产生这样的结果:

XXXX-XXXX-XAXA
XXXX-XXXX-XAXB
XXXX-XXXX-XAXC
XXXX-XXXX-XAXD
XXXX-XXXX-XAXE
XXXX-XXXX-XAXF
XXXX-XXXX-XAXG
(...)
XXXX-XXXX-XZX8
XXXX-XXXX-XZX9
XXXX-XXXX-X0XA
(...)
XXXX-XXXX-X2XA
XXXX-XXXX-X2XB

我知道锄头可以用一个“_”来完成。

                string alphaLetters = "ABCDEFGHIJKLMNOPQRSTUWXYZ0123456789ABCDEF";
                foreach (char letter in alphaLetters.ToCharArray())
                {
                    Numbers.Add(number.Replace('_', letter)));                     
                }

我希望这段代码使用未知数量的“_”。

你能帮忙吗?

【问题讨论】:

  • @Dyppl 其生成的字符串集合。
  • @Hooch:我正在编写另一个解决方案来解决您的问题
  • 号码 os _ 对于 Numbers 集合中的所有号码是否都相同?
  • @Dyppl 你不明白。 Numbers 是包含所有生成代码的集合。只有带有“_”的代码。这是用作模板的那个。
  • 您需要所有可能的组合吗?例如,如果 alphaLetters 的长度为 42,并且您有两个 _ 占位符,您想要 42*42 = 1764 个组合吗?

标签: c# algorithm function logic


【解决方案1】:

恕我直言,它必须是递归的。 (注意:这并不意味着必须使用递归方法调用,虽然我在下面的代码中使用了递归调用,但可以很容易地转换为内部递归堆栈。)

public static void RunSnippet()
{
    var r = new List<string>();
    Replace("asd_asd_asd_".ToCharArray(), 0, r);
    foreach(var s in r) { Console.WriteLine(s); }
}

public static char[] possibilities = new char[] { 'A', 'B', 'C' };

public static void Replace(char[] chars, int startIndex, IList<string> result)
{       
    for (int i = startIndex; i < chars.Length; i++)
    {
        if (chars[i] != '_')
        {
            continue;
        }

        // we found first '_'
        for (int j = 0; j < possibilities.Length; j++)
        {
            chars[i] = possibilities[j];
            Replace(chars, i + 1, result);              
        }

        chars[i] = '_'; // take back what we replaced
        return; //we're done here
    }

    // we didn't find any '_', so all were replaced and we have result:
    result.Add(new string(chars));
}

【讨论】:

    【解决方案2】:

    试试这个:

    var alphaIndexes = new List<int>();
    string alphaLetters = "ABCDEFGHIJKLMNOPQRSTUWXYZ0123456789ABCDEF";
    
    for(int n = 0; n<Numbers.Count; n++) {
        char[] numberLetters = Numbers[n].ToCharArray();
    
        int position = 0;
    
        for(int i = numberLetters.Length - 1; i>=0; i--) {  
            if(numberLetters[i] == '_') {
                int alphaIndex = 0;
                if(alphaIndexes.Count <= position)
                    alphaIndexes.Add(0);
                else {
                    alphaIndex = alphaIndexes[position];
                }
    
                numberLetters[i] = alphaLetters[alphaIndex];
                position++;         
            }
        }
    
        if(alphaIndexes.Count > 0) {
            alphaIndexes[0]++;
    
            for(int j = 0; j < alphaIndexes.Count; j++) {
                if(alphaIndexes[j] >= alphaLetters.Length) {
                    alphaIndexes[j] = 0;
                    if (j < alphaIndexes.Count)
                        alphaIndexes[j+1]++;
                }
            }
        }
        Numbers[n] = new String(numberLetters);
        Numbers[n].Dump();
    }
    

    【讨论】:

    • 但这可能也不是你需要的,抱歉,只是你的问题不是很清楚,我现在正在处理你的澄清
    猜你喜欢
    • 1970-01-01
    • 2012-04-18
    • 2022-11-14
    • 2022-12-18
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 2018-10-30
    • 2015-12-17
    相关资源
    最近更新 更多