【问题标题】:Word Conversion - Given Number All Possible Combination And Get Alphabet Base On Number单词转换 - 给定数字所有可能的组合并根据数字获取字母表
【发布时间】:2022-12-06 20:57:58
【问题描述】:

我有字母字典我想从基于数字的字典中获取字母表。我根据数字进行组合,但无法生成此结果。假设我给数字 5,它将从 1 到 5 开始并生成数字组合。我坚持从字典中抓取字母表。下面给出了示例。

示例:类似于 512

我们给出 2 个结果,例如: 5 1 2 = e a b 5 12 = 电子升

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NumberToWords
{
    public class Data
    {
        public IDictionary<int, string> listOfAlphabets;
        public Data()
        {
            listOfAlphabets = new Dictionary<int, string>();
            listOfAlphabets.Add(1, "A");
            listOfAlphabets.Add(2, "B");
            listOfAlphabets.Add(3, "C");
            listOfAlphabets.Add(4, "D");
            listOfAlphabets.Add(5, "E");
            listOfAlphabets.Add(6, "F");
            listOfAlphabets.Add(7, "G");
            listOfAlphabets.Add(8, "H");
            listOfAlphabets.Add(9, "I");
            listOfAlphabets.Add(10, "J");
            listOfAlphabets.Add(11, "K");
            listOfAlphabets.Add(12, "L");
            listOfAlphabets.Add(13, "M");
            listOfAlphabets.Add(14, "N");
            listOfAlphabets.Add(15, "O");
            listOfAlphabets.Add(16, "P");
            listOfAlphabets.Add(17, "Q");
            listOfAlphabets.Add(18, "R");
            listOfAlphabets.Add(19, "S");
            listOfAlphabets.Add(20, "T");
            listOfAlphabets.Add(21, "U");
            listOfAlphabets.Add(22, "V");
            listOfAlphabets.Add(23, "W");
            listOfAlphabets.Add(24, "X");
            listOfAlphabets.Add(25, "Y");
            listOfAlphabets.Add(26, "Z");
        }

    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NumberToWords
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int input = 1;
            Console.WriteLine("Please Add Number Of Set: ");
            input = Convert.ToInt32(Console.ReadLine());

            GenerateList(input);
        }
        public static void CheckFile()
        {
            string fileName = Environment.CurrentDirectory + "\\Result\\result.csv";
            try
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                File.Create(fileName); 
            }
            catch (Exception Ex)
            {
            }
        }
        static void GenerateList(int number)
        {
            var list = new List<int>();
            for (int i = 0; i < number; i++)
            {
                list.Add(i);
            }
            GetCombination(list);
        }
        static void GetCombination(List<int> list)
        {
            var data = new Data();
            var listOfAlphabets = data.listOfAlphabets;
            var csv = new StringBuilder();
            CheckFile();
            string output = "";
            double count = Math.Pow(2, list.Count);
            for (int i = 1; i <= count - 1; i++)
            {
                string str = Convert.ToString(i, 2).PadLeft(list.Count, '0');
                for (int j = 0; j < str.Length; j++)
                {
                    if (str[j] == '1')
                    {
                        try
                        {
                            //var charArray = str.ToCharArray();
                            //for (int k = 0;k< charArray.Length;k++)
                            //{
                            //    try
                            //    {
                            int xd = Convert.ToInt32(str[j]);
                                    output += listOfAlphabets.Where(x => x.Key == xd).Select(x => x.Value).FirstOrDefault();
                                    csv.AppendLine(output);
                            //    }
                            //    catch (Exception)
                            //    {

                            //    }
                            //}
                            
                           

                        }
                        catch (Exception)
                        {

                            throw;
                        }
                    }
                }
                Console.WriteLine();
            }

            File.WriteAllText(Environment.CurrentDirectory + "\\Result\\result.csv", csv.ToString());

        }
    }
}

【问题讨论】:

  • 你把这个练习的陈述重新表述得太糟糕了,在这个水平上,最好把它展示给我们看。

标签: c# .net numbers combinations permutation


【解决方案1】:

获得所有可能组合的一种方法是在递归算法中使用两个堆栈 inStackoutStack

在每一步中,您将一位数字从 inStack 移动到 outStack 并调用递归。
然后,如果来自 inStack 的第二个数字给出有效值(≤ 26),则使用组合值调用递归。
递归的停止条件是一个空的inStack

通话记录如下所示(堆栈头在右侧):

// Initial call
GetCombinations(inStack: '2 1 5', outStack: '')
|
|   // Pass 1 digit form inStack to outStack and call recursion
|-> GetCombinations(inStack: '2 1', outStack: '5')
    |
    |   // Pass 1 digit form inStack to outStack and call recursion
    |-> GetCombinations(inStack: '2', outStack: '5 1')
    |   |
    |   |   // Pass 1 digit form inStack to outStack and call recursion
    |   |-> GetCombinations(inStack: '', outStack: '5 1 2')  // -> returns 5 1 2
    |
    |   // Pass 2 combined digits form inStack to outStack and call recursion
    |-> GetCombinations(inStack: '', outStack: '5 12')       // -> returns 5 12

注意inStackoutStack的内容是相反的,Reverse必须在某个时候调用。

实现将如下所示:

static IEnumerable<IReadOnlyList<int>> GetCombinationsImplem(Stack<int> inStack, Stack<int> outStack)
{
    // recursion end point
    if (inStack.Count is 0)
    {
        yield return outStack.ToArray();
        yield break;
    }

    // Put one digit from in to out
    var digit1 = inStack.Pop();
    outStack.Push(digit1);

    // recursive call
    foreach (var combinaison in GetCombinationsImplem(inStack, outStack))
    {
        yield return combinaison;
    }

    // restore outStack
    outStack.Pop();


    // Try put two combined digit (ex: 1,2 => 12) from in to out
    // First, test for the presence of a digit
    if (inStack.TryPop(out var digit2))
    {
        var v = digit1 + 10 * digit2;

        // Continue only if the value is acceptable
        if (v <= 26)
        {
            outStack.Push(v);
            foreach (var combinaison in GetCombinationsImplem(inStack, outStack))
            {
                yield return combinaison;
            }

            // restore outStack
            outStack.Pop();
        }

        // restore inStack
        inStack.Push(digit2);
    }

    // restore inStack
    inStack.Push(digit1);
}

从这个转换字母是一个琐事(char)('a' + digit - 1)将1转换为a,2转换为b,等等......不需要字典。

可用的工作代码here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    相关资源
    最近更新 更多