求一个算法,我有一个数组{1,1,4,3,3},或{2,3,5,5,1}{1,1,4,3,3}:找出相同的数,并相加存下来{2,4,6}{2,3,5,5,1}:{2,3,10,1}

解决方案 »

  1.   

     int[] _Temp = new int[] { 1, 1, 4, 3, 3 };            
                IList<int> _Value = new List<int>();
                IList<int> _ValueCount = new List<int>();
                            for (int i = 0; i != _Temp.Length; i++)
                {
                    int _Index = _Value.IndexOf(_Temp[i]);
                    if (_Index == -1)
                    {
                        _Value.Add(_Temp[i]);
                        _ValueCount.Add(1);
                    }
                    else
                    {
                        _ValueCount[_Index]++;
                    }
                }            int[] _IntValue = new int[_Value.Count];            for (int i = 0; i != _Value.Count; i++)
                {
                    _IntValue[i] = _Value[i] * _ValueCount[i];
                }            
      

  2.   

    你把所有的int换成 double不就好了~~
      

  3.   

    谢谢,如果是{s,s,d,c,c}
    那又如何{ss,d,cc}
      

  4.   

     int[] ints = new int[] { 2, 3, 5, 5, 1 };
            int length = ints.Length;
            int[] ints2 = new int[length];
            int length2 = 0;
            for (int i = 0; i < length; i++)
            {
                int thisint = ints[i];
                bool isget = true;
                for (int j = 0; j < length2; j++)
                {
                    if (ints[j] == thisint)
                    {
                        isget = false;
                        break;
                    }
                }
                if (isget)
                {
                    int thisintsum = thisint;
                    for (int j = i + 1; j < length; j++)
                    {
                        if (ints[j] == thisint)
                        {
                            thisintsum += thisint;
                        }
                    }
                    ints2[length2] = thisintsum;
                    length2++;
                }
            }
            int[] ints3 = new int[length2];
            Array.Copy(ints2, 0, ints3, 0, length2);
      

  5.   

    string[] ints = new string[] { "2", "3", "5", "5", "1" };
            int length = ints.Length;
            string[] ints2 = new string[length];
            int length2 = 0;
            for (int i = 0; i < length; i++)
            {
                string thisint = ints[i];
                bool isget = true;
                for (int j = 0; j < length2; j++)
                {
                    if (ints[j] == thisint)
                    {
                        isget = false;
                        break;
                    }
                }
                if (isget)
                {
                    string thisintsum = thisint;
                    for (int j = i + 1; j < length; j++)
                    {
                        if (ints[j] == thisint)
                        {
                            thisintsum += thisint;
                        }
                    }
                    ints2[length2] = thisintsum;
                    length2++;
                }
            }
            string[] ints3 = new string[length2];
            Array.Copy(ints2, 0, ints3, 0, length2);
    //改下变量类型就好
      

  6.   

    net3.0以上版本直接使用linq就可以了 
    var Girls = new[] {"Christine", "Eva", "Jean", "Novia", "Winnie"};
    var PMs = new[] {"Eva", "Novia", "Vincent", "Williams", "Winnie"};  
     // 是 Girl 且是 PM (交集) 
    foreach (var s in Girls.Intersect(PMs))  
    {   
    Console.WriteLine(s
    }  
    Console.WriteLine();如果是net2.0的话,请参考 
    http://blog.blueshop.com.tw/uni2tw/archive/2008/02/14/54315.aspx 
      

  7.   

    有点乱不过功能实现了。int[] a = { 1, 3, 4,4,1, 1, 3, 3 };                       List<int> aa = new List<int>();
                List<int> bb = new List<int>();
                List<int> ll = new List<int>();
                for (int i = 0; i < a.Length; i++)
                {
                    aa.Add(a[i]);
                }         
                int cou = 0;
               
                for (int i = 0; i < aa.Count; i++)
                {
                    while ((len = aa.IndexOf(aa[i], ++len)) >= 0)
                    {
                        cou++;
                        ll.Add(aa[len]);
                    }
                    if (cou != 0)
                    {
                        bb.Add(cou * aa[i]);
                    }
                    else
                    {
                        bb.Add(aa[i]);
                    }
                    cou = 0;                for (int j = 0; j < ll.Count; j++)
                    {
                        aa.Remove(ll[j]);
                    }                               
                }
      

  8.   

    少写了个变量:int len = 0;cou后面
      

  9.   

    晕了,int len = -1;
      

  10.   

    妈的,我认为这个有点简单却用了一上午的时间,唉~~~~~~~~~~
    List<string> int_33 = new List<string> ( );//重复的数
                List<string> int_11 = new List<string> ( );//不重复的数            List<string> ints = new List<string> { "2" , "2" , "1" , "5" , "5" , "2" , "2","3" };
                List<string> ints_1 = new List<string> { "2" , "5" };
                foreach ( string s in ints_1 )
                {
                    foreach ( string str in ints )
                    {
                        if ( s == str )
                            int_11.Add ( str );                
                    }
                    string sTmp = "";
                    if ( int_11.Count > 0 )
                    {
                        foreach ( string stri in int_11 )
                        {
                            sTmp += stri;
                        }
                        int_33.Add ( sTmp );
                        int_11.Clear ( );
                    }
                }
                bool b = false;
                foreach ( string s in ints )
                {
                    foreach ( string str in ints_1 )
                    {
                        if ( s == str )
                        {
                            b = true;
                            continue;
                        }
                    }
                    if ( b == false )
                    {
                        int_11.Add ( s );
                    }
                    b = false;
                }
    还是要谢谢各位,把分数分了
      

  11.   

    List<string> int_33 = new List<string> ( );//重复的数
                List<string> int_11 = new List<string> ( );//不重复的数            List<string> ints = new List<string> { "2" , "2" , "1" , "5" , "5" , "2" , "2","3" };
                List<string> ints_1 = new List<string> { "2" , "5" };
                foreach ( string s in ints_1 )
                {
                    foreach ( string str in ints )
                    {
                        if ( s == str )
                            int_11.Add ( str );                
                    }
                    string sTmp = "";
                    if ( int_11.Count > 0 )
                    {
                        foreach ( string stri in int_11 )
                        {
                            sTmp += stri;
                        }
                        int_33.Add ( sTmp );
                        int_11.Clear ( );
                    }
                }
                bool b = false;
                foreach ( string s in ints )
                {
                    foreach ( string str in ints_1 )
                    {
                        if ( s == str )
                        {
                            b = true;
                            continue;
                        }
                    }
                    if ( b == false )
                    {
                        int_11.Add ( s );
                    }
                    b = false;
                }