Johnway203 2022-03-18 06:29 采纳率: 50%
浏览 93
已结题

c#环境下,一个数据结构的问题该如何编程

img


如图所示:
C#环境下
同一行表示“相互联通”
输入为左边一列,和右边一列,比如左边为 str1[]={“A”,“A”,“C”,“E”,“F”}
最后结果把所有联通的归到一起,
所以输出应当为{{“A”,“B”,“C”,“D”},{“E”,“F”,“G”} }
这个该如何编程?

  • 写回答

5条回答 默认 最新

  • soar3033 2022-03-18 08:13
    关注

    img

    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                string[] str1 = { "A", "A", "C", "E", "F" };
                string[] str2 = { "B", "C", "D", "F", "G" };
                int[] re = { 1, 0, 0, 0, 0, };
                int index = 2;
                for (int i = 1; i < str1.Length; i++)
                {
                    int st = 0;
                    for (int j = 0; j <= i-1; j++)
                    {
                        if((str1[i]==str1[j]) || (str2[i]==str1[j]) || (str1[i] == str2[j]) || (str2[i]==str2[j]))
                        {
                            re[i] = re[j];
                            st = 1;
                            break;
                        }
                    }
                    if (st == 0)
                    {
                        re[i] = index;
                        index++;
                    }
                }
                List<List<string>> res= new List<List<string>>();
                for (int i = 0; i < index-1; i++)
                {
                    res.Add(new List<string>());
                }
                for (int i = 0; i < str1.Length; i++)
                {
                    if (!res[re[i]-1].Contains(str1[i]))
                    {
                        res[re[i] - 1].Add(str1[i]);
                    }
                    if (!res[re[i] - 1].Contains(str2[i]))
                    {
                        res[re[i] - 1].Add(str2[i]);
                    }
                        
                }
                Console.Write("{");
                for (int i = 0; i < res.Count; i++)
                {
                    Console.Write("{");
                    for (int j = 0; j < res[i].Count; j++)
                    {
                        Console.Write(res[i][j]);
                        if (j==res[i].Count-1)
                        {
                            break;
                        }
                        Console.Write(",");
                    }
                    
                    if (i==res.Count-1)
                    {
                        Console.Write("}");
                    }
                    else
                    {
                        Console.Write("},");
                    }
                }
                Console.Write('}');
                Console.ReadKey();
            }
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 3月26日
  • 已采纳回答 3月18日
  • 赞助了问题酬金5元 3月18日
  • 创建了问题 3月18日

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用