C# 组合 交集,排除问题 救助!!!

名称名称 2015-12-22 05:19:37
菜鸟遇到两个问题了,谢谢。
问题1:textbox1.text="1234"
显示全部组合到textbox2中 123 124 134 234
问题1:
文本框1 内容格式 文本框二
123 124 134 234 空间分割的 123 124 134 234 567 789 012 空间分割的

交集和 文本框2 排除文本框1 的算法,谢谢大神~~~~!
...全文
381 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
KnitsEDI 2015-12-24
  • 打赏
  • 举报
回复
不错,学习学习。
编程有钱人了 2015-12-24
  • 打赏
  • 举报
回复
把字符串转成 string[] 数组 或者list集合 然后很简单了

List<string> ListA = new List<string>();
List<string> ListB = new List<string>();
List<string> ListResult = new List<string>();
 

ListResult = ListA.Distinct().ToList();//去重
ListResult = ListA.Except(ListB).ToList();//差集
ListResult= ListA.Union(ListB).ToList();  //并集
ListResult = ListA.Intersect(ListB).ToList();//交集

名称名称 2015-12-24
  • 打赏
  • 举报
回复
query.ToList().ForEach(q => this.textBox2.Text = textBox2.Text + "\r\n" + q.result); 解决了 会用 上面的main了 可是 结果出乎预料。 出现重复 比如112 212等
名称名称 2015-12-24
  • 打赏
  • 举报
回复
这个组合方法 出点问题用不成了 string a="123"; string b="345678" string c="567“ 这种情况 怎么获取 三位全组合或全排列 比如结果是 135 136 137 145 146 147 155 156 157 ...187 235 236 237 ...................................................287 335 336 337....................................................387
名称名称 2015-12-23
  • 打赏
  • 举报
回复
int[] a = { 1, 2, 3 }; int[] b = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int[] c = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 列出 不重复的组合 比如112 121 算重复
名称名称 2015-12-23
  • 打赏
  • 举报
回复
错误 1 当前上下文中不存在名称“Regex” d:\用户目录\我的文档\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 23 24 WindowsFormsApplication1
名称名称 2015-12-23
  • 打赏
  • 举报
回复
[quote=引用 6 楼 kusan888 的回复:] 谢谢6楼 和16楼 交集 排除 解决了,现在不重复组合怎么实现 8楼的帖子看到了 void Main() { List<List<string>> list = new List<List<string>>() { new List<string>() { "a" }, new List<string>() { "c", "d" }, new List<string>() { "e", "f", "g" }, new List<string>() { "h", "i" }, new List<string>() { "j" } }; var query = (from a in list.FirstOrDefault() from b in list.ElementAt(1) from c in list.ElementAt(2) from d in list.ElementAt(3) from e in list.LastOrDefault() select new { result = a + " " + b + " " + c + " " + d + " " + e }).Distinct(); query.ToList().ForEach(q => Console.WriteLine(q.result)); /* a c e h j a c e i j a c f h j a c f i j a c g h j a c g i j a d e h j a d e i j a d f h j a d f i j a d g h j a d g i j */ } 我菜鸟 不知道 怎么把结果显示在textbox里面 ,我怎么用这个main 怎么把结果显示在textbox里面 。 谢谢~~~~~~~~~~~~~~
名称名称 2015-12-23
  • 打赏
  • 举报
回复
[quote=引用 6 楼 kusan888 的回复:] 谢谢6楼 和16楼 交集 排除 解决了,现在不重复组合怎么实现 8楼的帖子看到了 void Main() { List<List<string>> list = new List<List<string>>() { new List<string>() { "a" }, new List<string>() { "c", "d" }, new List<string>() { "e", "f", "g" }, new List<string>() { "h", "i" }, new List<string>() { "j" } }; var query = (from a in list.FirstOrDefault() from b in list.ElementAt(1) from c in list.ElementAt(2) from d in list.ElementAt(3) from e in list.LastOrDefault() select new { result = a + " " + b + " " + c + " " + d + " " + e }).Distinct(); query.ToList().ForEach(q => Console.WriteLine(q.result)); /* a c e h j a c e i j a c f h j a c f i j a c g h j a c g i j a d e h j a d e i j a d f h j a d f i j a d g h j a d g i j */ } 我菜鸟 不知道 怎么把结果显示在textbox里面 ,我怎么用这个main 怎么把结果显示在textbox里面 。 谢谢~~~~~~~~~~~~~~
名称名称 2015-12-23
  • 打赏
  • 举报
回复
7楼能不能 帮我看一下 这个代码 报错 我是菜鸟 谢谢你了。交集 和text2排除text1 显示在textBox1,和textBox2中, 然后如果textBox1和textBox2如果有重复的项怎么把它删掉,重新格式化 每个三位数一个空格分割的格式,谢谢。 string text1 = "123 124 134 234"; string text2 = "123 124 134 234 567 789 012"; var arr1 = Regex.Split(text1, @"\s+"); var arr2 = Regex.Split(text2, @"\s+"); var arr3 = arr2.Except(arr1);//差集 Console.WriteLine(string.Join(" ", arr3)); textBox1.Text = string.Join(" ", arr3); var arr5 = arr2.Intersect(arr1);//交集 Console.WriteLine(String.Join(" ", arr5)); textBox2.Text = string.Join(" ", arr3);
q107770540 2015-12-23
  • 打赏
  • 举报
回复
排列组合: http://bbs.csdn.net/topics/360265119
  • 打赏
  • 举报
回复
string text1 = "123 124 134 234";
string text2 = "123 124 134 234  567 789 012";
var arr1 = Regex.Split(text1, @"\s+");
var arr2 = Regex.Split(text2, @"\s+");
var arr3 = arr2.Except(arr1);//差集
Console.WriteLine(string.Join(" ", arr3));
var arr5 = arr2.Intersect(arr1);//交集
Console.WriteLine(string.Join(" ", arr5));
显示你只要string.Join哪部分就可以了,Console.WriteLine是在控制台输出
xuzuning 2015-12-23
  • 打赏
  • 举报
回复
Console.WriteLine(string.Join(" ", arr3)); 中 Console.WriteLine 是控制台输出 string.Join(" ", arr3) 才是你要的结果 textbox.text = string.Join(" ", arr3) 不就显示到文本框中了吗
名称名称 2015-12-23
  • 打赏
  • 举报
回复
不管显示在什么地方 我怎么显示?我把代码贴上去报错 这个代码难道不在form 里面写?
早起晚睡 2015-12-23
  • 打赏
  • 举报
回复
别在textbox中显示 不好用listbox中比那个好
名称名称 2015-12-23
  • 打赏
  • 举报
回复
我就是不会用 ,我刚入门 我需要最终 显示textbox.text= ... 这个 这个代码我不会用啊
xuzuning 2015-12-23
  • 打赏
  • 举报
回复
求组合?算法 #8 已经给出了
名称名称 2015-12-23
  • 打赏
  • 举报
回复
自己顶 自己顶
名称名称 2015-12-22
  • 打赏
  • 举报
回复
四楼 这个事两个的排除 还是交集? Console.WriteLine(string.Join(" ", arr3)); 这个 怎么在textbox上显示? 全组合呢,谢谢
  • 打赏
  • 举报
回复
string text1 = "123 124 134 234";
string text2 = "123 124 134 234  567 789 012";
var arr1 = Regex.Split(text1, @"\s+");
var arr2 = Regex.Split(text2, @"\s+");
var arr3 = arr2.Except(arr1);
Console.WriteLine(string.Join(" ", arr3));
是这个意思吗?
名称名称 2015-12-22
  • 打赏
  • 举报
回复
显示在textbox中,
加载更多回复(2)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧