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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测