【问题标题】:Print list items打印列表项
【发布时间】:2015-01-08 15:32:48
【问题描述】:
List<string> list = new List<string>();    
        list.Add("A");
        list.Add("B");

List<string> list1 = new List<string>();    
        list.Add("a");
        list.Add("b");


    for (int i = 0; i < list.Count; i++)
    {
        // print another list items.
        for (int j = 0; j < list1.Count; j++)
        {
            Console.WriteLine("/" + list[i] + "/" + list1[j]);
        }

    }

我想像这样string tmpS =+ list[i]; 编码来加入下一个列表项。

然后打印tmpS

但编译错误 CS0023:运算符“+”不能应用于“字符串”类型的操作数。

如何打印以下所有项目。(任何类型都可以)

一个 氨基酸 抗体 Aab 阿坝 AB ABA 抗体 抗体 阿巴 乙 巴 BB 巴布 巴巴

(大写数字不交换。小字符应该交换。并且总是在大写数字后附加小字符。)

【问题讨论】:

  • 您确定 Aab 必须在 AB 部分吗?
  • 你好。没有不同的部分。只需要上面所有的组合项目。 List 将他的项目组合起来,并将他的项目与其他列表项目组合起来。

标签: c# data-structures


【解决方案1】:

这让我很长时间没有解决纯粹的算法问题!

这个程序应该可以解决问题:

class Program
{
    static void Main(string[] args)
    {
        List<string> uppers = new List<string>();
        uppers.Add("A");
        uppers.Add("B");

        List<string> lowers = new List<string>();
        lowers.Add("a");
        lowers.Add("b");

        List<string> combinedUppers = GetCombinedItems(uppers);
        List<string> combinedLowers = GetCombinedItems(lowers);
        List<string> combinedUppersLowers = GetCombinedList(combinedUppers, combinedLowers);

        foreach (string combo in combinedUppersLowers)
        {
            Console.WriteLine(combo);
        }

        Console.Read();
    }

    static private List<string> GetCombinedItems(List<string> list)
    {
        List<string> combinedItems = new List<string>();

        for (int i = 0; i < list.Count; i++)
        {
            combinedItems.Add(list[i]);

            for (int j = 0; j < list.Count; j++)
            {
                if (list[i] != list[j])
                {
                    combinedItems.Add(String.Format("{0}{1}", list[i], list[j]));
                }
            }
        }

        return combinedItems;
    }

    static private List<string> GetCombinedList(List<string> list1, List<string> list2)
    {
        List<string> combinedList = new List<string>();

        for (int i = 0; i < list1.Count; i++)
        {
            combinedList.Add(list1[i]);

            for (int j = 0; j < list2.Count; j++)
            {
                combinedList.Add(String.Format("{0}{1}", list1[i], list2[j]));
            }
        }

        for (int i = 0; i < list2.Count; i++)
        {
            combinedList.Add(list2[i]);

            for (int j = 0; j < list1.Count; j++)
            {
                combinedList.Add(String.Format("{0}{1}", list2[i], list1[j]));
            }
        }

        return combinedList;
    }
}

问候。


这个程序给你这个输出:

一个 氨基酸 Aab 抗体 阿坝 AB ABA 抗体 抗体 阿巴 乙 巴 巴布 BB 巴巴 文学学士 BAa 巴布 抗体 巴巴 一个 氨基酸 aAB 甲乙 ABA 抗体 抗体 抗体 ab abBA b bA bAB bB bBA 巴 呱呱 巴比 巴比 巴巴

【讨论】:

  • @TheRHCP。当我为 List1 添加 A、B、C 时,我测试了为 List1 和 List2 添加的更多项目;列表 2 的 a、b、c;脚本不能打印超过 4 个字符。(如 ABCabc / ACADc/ ABCab...不能打印。我想要做的是结合 List1 和 List2 的所有条件)。目前仅支持 NewString
  • 我给你的算法不能按列表处理超过 2 个字符。要解决此问题,您必须调整 GetCombinedItems 方法。我认为它可以很容易地适应,但要实现的算法设计起来有点复杂,因为它必须处理未知数量的字符。其实这个方法只是生成一个所有可能的字符组合的列表,我想你可以在网上找到这种算法。
【解决方案2】:

这闻起来像家庭作业。

List<string> list = new List<string>();    
list.Add("A");
list.Add("B");

List<string> list1 = new List<string>();    
list.Add("a");
list.Add("b");

string xxx = "";
for (int i = 0; i < list.Count; i++)
{
    xxx += list[i];
    Console.WriteLine(xxx);

    // print another list items.
    for (int j = 0; j < list1.Count; j++)
    {
        Console.WriteLine("/" + list[i] + "/" + list1[j]);
    }

}

【讨论】:

  • 你好 Slugster,跟随你的指南。我发现有些项目仍然无法打印。
【解决方案3】:

这是+= 而不是=+

但无论如何你都应该使用 StringBuilder。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 2022-08-08
    • 2013-03-24
    • 1970-01-01
    相关资源
    最近更新 更多