有两个排好序的数组,如何比较两个数组相同的元素个数,最好不使用两个循环.

解决方案 »

  1.   

    for(int cnt;cnt < strA.Length && cnt < strB.Length;cnt++)
    {
       if(strA[cnt] != strB[cnt])
          MessageBox.Show("元素不一样!");
    }是这个意思么?比较元素?
      

  2.   

    假设2个数组
    A B简称爬杆法
    把B当成杆,A的元素当成爬杆者
    从A中拿最小的元素和B中从小到大元素比较
      如果查找到B中有比它大且不相当的元素,
      则从A中换一个元素接着爬
      

  3.   

    int i;
    for(int cnt;cnt < strA.Length && cnt < strB.Length;cnt++) 

       if(strA[cnt] != strB[cnt]) 
          MessageBox.Show("元素不一样!"); 
       else
          i++;
    }
    MessageBox.Show("strA于strB中相同元素的个数是:" + i.ToString());  z这样差不多了吧。
      

  4.   

                int cnt = 0;
                string[] strA = new string[10];
                string[] strB = new string[10];
                for (int j = 0; j < strA.Length; j++)
                {
                    for (int i = 0; i < strB.Length; i++)
                    {
                        if (strA[j] == strB[i])
                            cnt++;
                    }
                }
                MessageBox.Show("相同元素个数:" + cnt.ToString());
    这次对了,绕
      

  5.   

    zhchg6666 所说的爬杆法好像也要做两次循环,有没有不用循环两次的方法?数组是排好序的,有没有更好的方法
      

  6.   

    最好不使用两个循环.
    for和if是运行最快的了,多套几个不会影响速度的,而且简单,何必自找烦恼呢?
      

  7.   


        /// <re>
        ///  arrA and arrB should be already sorted
        /// </re>
        static void PrintSameElements(int[] arrA, int[] arrB)
        {
            int floor = 0;
            foreach (int b in arrB)
            {
                for (int i = floor; i < arrA.Length; i++)
                {
                    if (b == arrA[i]) { Console.WriteLine(b); break; }
                    else if (b < arrA[i]) { floor = i; break; }
                }
            }
        }
      

  8.   

    int EquallCount=0;
    int i,j=0;do
    {
       if(a[i]>b[j])
       {
         j++;
       }
       else if(a[i]<b[j])
       {
         i++;
       }
       else
       {
         count++
       }
    }while(i!=a.count||j!=b.count)return count;//楼主是这个意思吗?
      

  9.   


    This one is better :-)
      

  10.   

    更改了一些错误(原代码见10楼):        static int f(int[] a, int[] b)
            {
                int count = 0;
                int i = 0, j = 0;            while (i < a.Length && j < b.Length)
                {
                    if (a[i] > b[j])
                    {
                        j++;
                    }
                    else if (a[i] < b[j])
                    {
                        i++;
                    }
                    else
                    {
                        i++;
                        j++;
                        count++;
                    }
                }            return count;
            }
      

  11.   

    一个数组中元素可以重复吗?这样的话算不算重复count增加吗/int[] a = new int[] { 1, 3, 4, 5, 6, 6, 7 };
    int[] b = new int[] { 3, 3, 4, 6, 7, 9 };
      

  12.   

            static int compareTowArray(int[] nAAry, int[] nBAry)
            {
                int nCount = 0;
                int i = 0, j = 0;
                Boolean bASolved = false, bBSolved = false;
                while (!bASolved || !bBSolved)
                {
                    if ((i < nAAry.Length) && (j < nBAry.Length))
                    {
                        if (nAAry[i] <= nBAry[j])
                        {
                            if (nAAry[i] == nBAry[j])
                            {
                                nCount++;
                            }
                            i++;
                        }
                        else
                        {
                            j++;
                        }
                    }
                    else
                    {
                        if (i == nAAry.Length)
                        {
                            i--;
                            j++;
                            bASolved = true;
                            continue;
                        }                    if (j == nBAry.Length)
                        {
                            j--;
                            i++;
                            bBSolved = true;
                            continue;
                        }
                    }
                }            return nCount;
            }
      

  13.   

    并不简单啊,修改版        static int compareTowArray(int[] nAAry, int[] nBAry)
            {
                int nCount = 0;
                int i = 0, j = 0;
                Boolean bASolved = false, bBSolved = false;
                int nRecordi = -2, nRecordj = -2;
                while (!bASolved || !bBSolved)
                {
                    if ((i < nAAry.Length) && (j < nBAry.Length))
                    {
                        if (nAAry[i] <= nBAry[j])
                        {
                            if (nAAry[i] == nBAry[j])
                            {
                                if (!(nRecordi + 1 == i && nRecordj == j) && !(nRecordj + 1 == j && nRecordi == i))
                                {
                                    nCount++;
                                    nRecordi = i;
                                    nRecordj = j;
                                }
                            }
                            i++;
                        }
                        else
                        {
                            j++;
                        }
                    }
                    else
                    {
                        if (i == nAAry.Length)
                        {
                            i--;
                            j++;
                            bASolved = true;
                            continue;
                        }                    if (j == nBAry.Length)
                        {
                            j--;
                            i++;
                            bBSolved = true;
                            continue;
                        }
                    }
                }            return nCount;
            }