c# 字符串数组中元素两两组合问题,求高手指点

yitui356 2012-08-17 12:10:32
字符串数组arraylianhao{"1","12","3","34","45"}
其中元素组合后放入字符串变量xinzifu。
组合规则如下:
第一轮:arraylianhao[0]依次与其他元素组合,得到“1,12”,“1,3”,“1,34”,“1,45” arraylianhao[1]依次与其他元素组合,得到 “12,3”,“12,34”,“12,45”
arraylianhao[2]依次与其他元素组合,得到 “3,34”,“3,45”
arraylianhao[3]依次与其他元素组合,得到 “34,45”
将得到字符串删去重复,得到“1,12”,“1,3”,“1,34”,“1,45”,“12,3”,“12,34”,“12,45”,“3,34”,“3,45”,“34,45”
第二轮:“1,12”和“1,3”组合后删除重复字符“1”,得到“1,12,3”。“1,12”和“1,34”组合后删除重复字符,得到“1,12,34”依次类推
下去

代码:for(int i=0;i<arraylianhao.length-1;i++)
for(int j=i+1;j<arraylianhao.length;j++)
string xinzifu=arraylianhao[i]+","+arraylianhao[j];
xinzifu=regex.replace(xinzifu,"(?s)(.)(?=.*\\1)","");
例如应该得到“1,12”,却是“,12”。程序把重复的“1”删掉了
但是在第二轮,又要保持“1,12”和“1,3”组合时删掉重复的“1”。

如何解决?

...全文
855 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
tiny_god 2012-08-18
  • 打赏
  • 举报
回复
楼主,你要的结果是什么,答案运行出来感觉不对吧,比如1,12,3出来的结果已经是排过序的
yitui356 2012-08-18
  • 打赏
  • 举报
回复
问题解决,
多谢各位热心相助,感激……
  • 打赏
  • 举报
回复
你期望的结果是什么?
yitui356 2012-08-17
  • 打赏
  • 举报
回复
谢谢ls兄弟,但和目标差的太远,
  • 打赏
  • 举报
回复
var arraylianhao = new string[] { "1", "12", "3", "34", "45" };
string xinzifu="";
for(int i=0;i<arraylianhao.Length;i++)
for(int j=i+1;j<arraylianhao.Length;j++)
xinzifu+=arraylianhao[i]+","+arraylianhao[j]+"\n";
Console.WriteLine(xinzifu);
zhanglong19891129 2012-08-17
  • 打赏
  • 举报
回复
最下面这个应该是你需求的
zhanglong19891129 2012-08-17
  • 打赏
  • 举报
回复
string[] myarray = new string[] { "1", "12", "3", "34", "45" };
var result = from a in myarray
from b in myarray
where Convert.ToInt32(a) < Convert.ToInt32(b)
select string.Format("{0},{1}", a, b);
foreach (var aaaaa in result)
{Console.WriteLine(aaaaa.ToString());}
Console.WriteLine("------------------------");
var result1 = from a in myarray
from b in myarray
from c in myarray
where Convert.ToInt32(a) < Convert.ToInt32(b) &&Convert.ToInt32(b)< Convert.ToInt32(c)
select string.Format("{0},{1},{2}", a, b,c);
foreach (var bbb in result1)
{
Console.WriteLine(bbb.ToString());
}
Console.ReadLine();
zhanglong19891129 2012-08-17
  • 打赏
  • 举报
回复
arraylianhao{"1","12","3","34","45"}
array[] record=arraylianhao.toarray();
var result1 = from a in record
from b in record
where a!=b
select string.fomart("{0} , {1}",a.tostring,b.tostring);
这个得到的结果就是 两两组合的结果.
var result2=from a in record
from b in record
from c in record
where a!=b and a!=c and b!=c
select string.fomart("{0},{1},{2}",a.tostring(),b.tostring(),c.tostring());
这个得到的结果就是你第3次要求的结果
tiny_god 2012-08-17
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string[] src = { "1", "12", "3", "34", "45" };
List<string> temp = new List<string>();
List<string> temp_second = new List<string>();
string collect="";
for (int i = 0; i < src.Length - 1;i++ )
{
for (int j = 1; j < src.Length - i;j++ )
{
string tp=src[i] + "," + src[i + j];
if (!collect.Contains(tp+";"))
{
collect = collect + tp + ";";
temp.Add(tp);
}
}
}
Console.WriteLine("总数{0}", temp.Count);
Console.WriteLine("第一次结果:");
int n = 0;
foreach (string t in temp)
{
n++;
if (n >= 5)
{
Console.Write("\n");
n = 0;
}
Console.Write(t+" ");
}
collect = "";
for (int i = 0; i < temp.Count - 1; i++)
{
for (int j = 1; j < temp.Count-i; j++)
{
collect = temp[i];
string[] temp1=temp[i + j].Split(',');
if (!(collect.Contains(temp1[0]+",")|collect.Contains(","+temp1[0])))
{
collect = collect +","+ temp1[0] ;
}
if (!(collect.Contains(temp1[1] + ",") | collect.Contains("," + temp1[1])))
{
collect = collect + "," + temp1[1];
}
temp_second.Add(collect);
}
}
Console.WriteLine("\n第二次总数{0}", temp_second.Count);
Console.WriteLine("第二次结果:");
int m=0;
foreach (string t in temp_second)
{
m++;
if (m>=5)
{
Console.Write("\n");
m = 0;
}
Console.Write(t + " ");
}
Console.ReadKey();
}
}
}
直接运行就成,第一次结果保存在list temp中,第二次结果保存在list temp_second中,应该符合你的要求
shizhu820228 2012-08-17
  • 打赏
  • 举报
回复

private string[] arraylianhao = { "1", "12", "3", "34", "45" };
private List<string> lstStr;
public Form26()
{
InitializeComponent();
timeLoop(3);
}

void timeLoop(int loop)
{
for (int i = 0; i < loop; i++)
{
cal();
}
}
void cal()
{
string tempA;
lstStr = new List<string>();
for (int i = 0; i < arraylianhao.Length -1; i++)
{
for (int j = i+1; j < arraylianhao.Length; j++)
{
tempA = arraylianhao[i] + "," + arraylianhao[j];
tempA = string.Join(",", tempA.Split(',').Distinct().ToArray());
lstStr.Add(tempA);
}
}
arraylianhao = lstStr.Distinct().ToArray();
}

111,097

社区成员

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

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

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