当前位置:编程学习 > C#/ASP.NET >>

c#数组问题请教

我现在有两个数组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没有的数,

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

--------------------编程问答-------------------- 数组貌似从0开始的吧
楼主先看看数据结构中的查找一章,思路就很明显了。

--------------------编程问答-------------------- 数组是什么数组?
假设是string 吧.
foreach(string s in a)
{
  int j=B.GetLength,z=0
  for(int i=0;i<j;i++)
  {
    if(s == b[i])
     {
        c[z] = b[i];
        z++;
     }
  }
} --------------------编程问答-------------------- 哦错了..
是if(s != b[i]).. --------------------编程问答-------------------- 将A,B分别插入到数据库
然后select * from a left join b on a.value!=b.value --------------------编程问答--------------------    呵呵,学习了 --------------------编程问答-------------------- 如果是VS2008则可以直接使用LINQ --------------------编程问答--------------------

using System.Linq;
int[] a = { 1, 2, 3, 4, 5, 9 };
int[] b = { 1, 4, 5, 7, 8, 9 };
var c = a.Cast<int>().Concat(b.Cast<int>()).GroupBy((t) => t).Where((t) => t.Count() == 1);
foreach (var d in c)
{
MessageBox.Show(d.Key.ToString());
}
--------------------编程问答-------------------- 7楼的Linq处理,你可以试试看。 --------------------编程问答-------------------- Linq的算法效率也未必高,应该是可以用Dictionary解决的 --------------------编程问答-------------------- 用 2 * n 替代 n * n

            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>();

            Dictionary<int, bool> dictionary = new Dictionary<int, bool>();

            for (int i = 0; i < a.Length; i++)
            {
                if(!dictionary.ContainsKey(a[i]))
                {
                    dictionary[a[i]] = true;
                }
            }

            for (int i = 0; i < b.Length; i++)
            {
                if (!dictionary.ContainsKey(b[i]))
                {
                    c.Add(b[i]);
                }
            }
            
            //c.ToArray()可以得到数组

            foreach (int item in c)
            {
                Console.Write(item + " ");
            }
--------------------编程问答-------------------- 高歌,学习了! --------------------编程问答-------------------- study...... --------------------编程问答-------------------- 学习中! --------------------编程问答-------------------- 在考虑有没有办法通过数学的方法简化算法……想到一个,不过条件很苛刻,实际工作中用处不大……自娱自乐而已

条件:两个数组里的数都是无重复整数且不超过63
1. 把两个数组的数字分别取2的幂,即保证每个数字的二进制码为1后面n个0
2. 两个数组分别求和,如totalA = 2 ^ a[0] + 2 ^ a[1] + ... + 2 ^ a[m - 1]
3. totalA 对 totalB 做蕴含运算,即前真后假为假,其余为真(貌似C#里没这个运算?)
4. 根据所得数字二进制码中0的位置判断哪些数字在A中不在B中。比如11001101就表示1,4,5不在B里 --------------------编程问答-------------------- 学习一下! --------------------编程问答-------------------- 高歌的方法 UP一下!!
对效率比较敏感的时候基本是摒弃了LINQ的 --------------------编程问答-------------------- 貌似高歌的没考虑完全哦。只是从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 --------------------编程问答-------------------- 加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();
}
--------------------编程问答-------------------- 10楼与17楼两楼的效率怎么样呢?
逍遥兄 Remove(n)方法也是让c数组循环来判断与输入的n值相同情况才删除,就是说也要进行循环?
--------------------编程问答--------------------
引用 19 楼 denbes 的回复:
10楼与17楼两楼的效率怎么样呢?
逍遥兄 Remove(n)方法也是让c数组循环来判断与输入的n值相同情况才删除,就是说也要进行循环?


没错,算法都是根据情况选择的,如果数据量大,就可以在循环之前对c,d先排序,搜索会快,因为会尽可能的从头上删除数据,循环能有效的控制在一个很低的数量上,但现在数据量不大,先排序,效果并不会有改善,反而会感觉的到排序的时间(100000次重复运行测试)。 --------------------编程问答-------------------- 引用 15 楼 sire168 的回复:
学习一下!
--------------------编程问答-------------------- 高手真多啊,帮顶下 --------------------编程问答-------------------- 学习了..... --------------------编程问答-------------------- 太多了,不想看了 --------------------编程问答-------------------- 恩,,不错,既然高手都用List或Dictionary了,说明C#里木有Set --------------------编程问答-------------------- 逍遥兄的这个方法很值得借鉴 --------------------编程问答-------------------- 学习ing。。 --------------------编程问答-------------------- 先将两个数组都排序好,然后以其中一个数组为基数组,对另外一个数组进行比较,如此,将大大减少比较次数,增加其速度。 --------------------编程问答--------------------

            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);
            }
--------------------编程问答-------------------- 两数据先排序,再比较,这样效率会高些。 --------------------编程问答--------------------


        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);
            }
        }

--------------------编程问答--------------------
引用 31 楼 iammac 的回复:
C# code


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

再加个判断

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


--------------------编程问答-------------------- 1、数组从0开始
2、先排序再对比 --------------------编程问答-------------------- 真是学习了。。高! --------------------编程问答--------------------
引用 34 楼 xky578353168 的回复:
真是学习了。。高!

同感 --------------------编程问答--------------------

    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
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,