有两个整形数组,A和B,A中的整数不能重复,且数量不确定。B中的整数可以重复,但是B中的整数一定是A中整数的一个。现在想要输出A中整数在B中的数量。如:
A:{1,2,3,4} B:{2,2,2,3,3,4,4,1}
想要输出:1,3,2,1
即:A中的1有1个,2有3个,3有2个,4有1个

解决方案 »

  1.   

    for(int i=0;i<a.Length;i++)
    {
    int k=0;
    for(int j=0;j<b.Length;j++)
    {
       if(a[i]==b[j])k++;
    }
    }
      

  2.   

    public static void M2()
    {
    int[] a = {1,2,3,4};
    int[] b = {2,2,2,3,3,4,4,1}; Hashtable h = new Hashtable( a.Length );

    // Loop array A
    foreach( int i in b )
    {
    if ( h.ContainsKey( i ) )
    {
    h[ i ] = (int)h[ i] + 1;
    }
    else
    {
    h.Add( i, 1 );
    }
    } // Loop Array A,print result out for( int i = 0; i < a.Length - 1; i++ )
    {
    if ( h.ContainsKey( a[ i ] ) )
    {
    Console.Write( "{0},", h[ a[ i ] ] );
    }
    }
    if ( h.ContainsKey( a[ a.Length - 1 ] ) )
    {
    Console.WriteLine( h[ a[ a.Length - 1 ] ]  );
    } }
      

  3.   

    建议,先定义一个新的数组,然后查找数组A的第一个元素在B中的个数,同时把B中不是A的第一个元素的数复制到新数组中.按此思想重复循环,查找A中的第二个数的时候只要把新数组中不是此数的数在复制到B中就可以,一直循环查找,你的问题就解决了.另外,当查找最后一个数的时候,你找要输出B或者新数组的长度就可以了,不知道对你解决问题有没有帮助
      

  4.   

    int[] a = {1,2,3,4};
    int[] b ={ 2,2,2,3,3,4,1};
    int[] c = new int[a.length];
    Array.Sort( a );
    Array.Sort( b );
    int i=0;
    int j=0;
    int k=0;
    for( int i=0 ; i<a.length ; i++)
    {
       for(  ; j<b.leng ; )
       {
           if( a[i]== b[j])
              c[i]++;
        }
        j += c[i];
    }