c#数组问题请教

jiangwei1983620 2007-08-18 10:55:11
我现在有两个数组A[m],B[n]我要做的是把两个数组中不同于另外一个数组中的数据分别取出来,我的方法比较笨.
for(i=1,i<m,i++)
for(j=1,j<n,j++)
{
if(A[m]!=b[n])
C[p]=A[m];
p++;
}
这里C[p]就是A中B没有的数,
for(j=1,j<n,j++)
for(i=1,i<m,i++)
{
if(B[n]!=A[m])
D[Q]=B[n];
Q++;
}

这里D[Q]就是B中A没有的数,

但我觉得这方法不是很好,应该有更简单的,请各位大虾指教,小弟是初学,最好能详细点

...全文
398 36 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
兔子-顾问 2010-07-15
  • 打赏
  • 举报
回复
加2行注释
static void Main(string[] args)
{
int[] a = new int[] { 1, 5, 8, 4, 2, 9 };
int[] b = new int[] { 2, 4, 6, 7, 8 };

List<int> c = new List<int>(a);
List<int> d = new List<int>(b);
foreach (int n in b)//O(a.Length)
{
c.Remove(n);
}
foreach (int n in a)//O(b.Length)
{
d.Remove(n);
}
//至此c表示a中有,b中没有的
//至此d表示b中有,a中没有的
c.AddRange(d);//功能至此结束下面是显示

//排序一下好看点
c.Sort();
c.ToList().ForEach(c1 => Console.Write(c1+" "));

Console.ReadKey();
}
兔子-顾问 2010-07-15
  • 打赏
  • 举报
回复
貌似高歌的没考虑完全哦。只是从a中屏蔽掉了b相关的,没有反过来哦。贴一个刚写的…

static void Main(string[] args)
{
int[] a = new int[] { 1, 5, 8, 4, 2, 9 };
int[] b = new int[] { 2, 4, 6, 7, 8 };

List<int> c = new List<int>(a);
List<int> d = new List<int>(b);
foreach (int n in b)//O(a.Length)
{
c.Remove(n);
}
foreach (int n in a)//O(b.Length)
{
d.Remove(n);
}
c.AddRange(d);//功能至此结束下面是显示

//排序一下好看点
c.Sort();
c.ToList().ForEach(c1 => Console.Write(c1+" "));

Console.ReadKey();
}


结果:
1 5 6 7 9
winbq5 2010-07-15
  • 打赏
  • 举报
回复
1、数组从0开始
2、先排序再对比
iammac 2010-07-15
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 iammac 的回复:]

C# code


static void Main(string[] args)
{
const int MAX = 10000;
const int LENGTH = 1000000;
int[] array1 = new int[LENGTH];
int[] a……
[/Quote]
再加个判断

for (int i = 0; i < LENGTH; i++)
{
if(p>=LENGTH)
break;
while (p<LENGTH)
.....


iammac 2010-07-15
  • 打赏
  • 举报
回复


static void Main(string[] args)
{
const int MAX = 10000;
const int LENGTH = 1000000;
int[] array1 = new int[LENGTH];
int[] array2 = new int[LENGTH];

//Fill
Random rd = new Random();
for (int t = 0; t < LENGTH; t++)
{
array1[t] = rd.Next(MAX);
array2[t] = rd.Next(MAX);
}
//Begin proccessing
//Sort
Quicksort(ref array1, 0, LENGTH - 1);
Quicksort(ref array2, 0, LENGTH - 1);

IList<int> result = new List<int>();
int p = 0;
for (int i = 0; i < LENGTH; i++)
{
while (p<LENGTH)
{
if (array2[i] < array1[p])
{
result.Add(array2[i]);
break;
}
else if (array2[i] > array1[p])
{
p++;
}
else
{
p++;
break;
}
}
}
}

private static void Quicksort(ref int[] array, int left, int right)
{
if (left < right)
{
int middle = array[(left + right) / 2];
int i = left - 1;
int j = right + 1;
while (true)
{
while (array[++i] < middle) ;
while (array[--j] > middle) ;
if (i >= j)
break;
Swap(ref array, i, j);
}
Quicksort(ref array, left, i - 1);
Quicksort(ref array, j + 1, right);
}
}

guojh021 2010-07-15
  • 打赏
  • 举报
回复
两数据先排序,再比较,这样效率会高些。
Peter200694013 2010-07-15
  • 打赏
  • 举报
回复

Int32[] a = new Int32[] { 1, 5, 8, 4, 2, 9 };
Int32[] b = new Int32[] { 2, 4, 6, 7, 8 };
Dictionary<Int32, String> d = new Dictionary<Int32, String>();

for (Int32 i = 0; i < a.Length; i++)
{
d.Add(a[i], "a");
}
for (Int32 j = 0; j < b.Length; j++)
{
if (d.ContainsKey(b[j]))
{
d[b[j]] = "c";
}
else
{
d.Add(b[j], "b");
}
}
//value为a则是数组A有,数组B没有;为b则是数组B有,数组A没有;为c则是都存在的,即交集
foreach (KeyValuePair<Int32, String> kvp in d)
{
if(kvp.Value == "a")
Console.WriteLine("{0} is just in Array A", kvp.Key);
if (kvp.Value == "b")
Console.WriteLine("{0} is just in Array B", kvp.Key);
}
liubing9802 2010-07-15
  • 打赏
  • 举报
回复
先将两个数组都排序好,然后以其中一个数组为基数组,对另外一个数组进行比较,如此,将大大减少比较次数,增加其速度。
hao494012564 2010-07-15
  • 打赏
  • 举报
回复
学习ing。。
zhoubupt 2010-07-15
  • 打赏
  • 举报
回复
逍遥兄的这个方法很值得借鉴
angel6709 2010-07-15
  • 打赏
  • 举报
回复
恩,,不错,既然高手都用List或Dictionary了,说明C#里木有Set
宇峰科技 2010-07-15
  • 打赏
  • 举报
回复
太多了,不想看了
I520WUCHAN 2010-07-15
  • 打赏
  • 举报
回复
学习了.....
Zz254274638 2010-07-15
  • 打赏
  • 举报
回复
引用 15 楼 sire168 的回复:
学习一下!
兔子-顾问 2010-07-15
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 denbes 的回复:]
10楼与17楼两楼的效率怎么样呢?
逍遥兄 Remove(n)方法也是让c数组循环来判断与输入的n值相同情况才删除,就是说也要进行循环?
[/Quote]

没错,算法都是根据情况选择的,如果数据量大,就可以在循环之前对c,d先排序,搜索会快,因为会尽可能的从头上删除数据,循环能有效的控制在一个很低的数量上,但现在数据量不大,先排序,效果并不会有改善,反而会感觉的到排序的时间(100000次重复运行测试)。
xrongzhen 2010-07-15
  • 打赏
  • 举报
回复

string[] strOne = { "1", "2", "3", "4", "5" };
string[] strTwo = { "1", "3", "5", "7", "9" };

List<string> l = strOne.Except<string>(strTwo).ToList<string>();

foreach(string s in l)
{
Console.WriteLine(s.ToString());
}
//结果:
//2
//4
永恒的小崔 2010-07-15
  • 打赏
  • 举报
回复
[Quote=引用 34 楼 xky578353168 的回复:]
真是学习了。。高!
[/Quote]
同感
xky578353168 2010-07-15
  • 打赏
  • 举报
回复
真是学习了。。高!
denbes 2010-07-15
  • 打赏
  • 举报
回复
10楼与17楼两楼的效率怎么样呢?
逍遥兄 Remove(n)方法也是让c数组循环来判断与输入的n值相同情况才删除,就是说也要进行循环?
  • 打赏
  • 举报
回复
高歌的方法 UP一下!!
对效率比较敏感的时候基本是摒弃了LINQ的
加载更多回复(15)

111,092

社区成员

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

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

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