解决方案 »

  1.   

    我只会这样写.
    var test = new[] {1, 3, 5, 3, 3, 1};
                var count = new Dictionary<int, int>();
                foreach (var t in test)
                {
                    if (count.ContainsKey(t))
                    {
                        count[t]++;
                    }
                    else
                    {
                        count.Add(t,1);
                    }
                }
                foreach (var item in count)
                {
                    Console.WriteLine("{0}->{1}",item.Key,item.Value);
                }
                Console.ReadLine();
      

  2.   

    int Min, Max, Num, Data; //随机数下界,上界,个数,Data临时存储随机数
                Dictionary<int, int> count = new Dictionary<int, int>(); //字典,以int为键保存次数。
                Random ra = new Random();
                for (int i=0;i<Num;i++)
                {
                    Data = ra.Next(Min, Max);
                    if (count.ContainsKey(Data)) count[Data]++;
                    else count.Add(Data, 1);
                }
      

  3.   


    请问能用hashtable实现吗?
      

  4.   

    说说也无妨,能不能用hashtable
      

  5.   

    Hashtable存储键值对Key/Value,其中Key要求唯一,而Value可以重复。你这种情况应该可以,在逻辑上好好想想,Key存储不同的值,用ContainsKey方法判断Key是否存在,若已存在,相应的Value值加1.
      

  6.   

    你随机生成“1,3,5,3,3,1“这个六个数。
    Hashtable hash = new Hashtable();
    int count = 1;  //重复次数
    for (int i = 0; i < arr.Length; ++i)
    {
           if (hash.ContainsKey(arr[i]))
           {
                  ++count;
                  hash.Add(arr[i], count);
           }
    }
    如果你要遍历出来:
             foreach (DictionaryEntry   dic    int   hash)
                      txtBoxHash.Text = dic.Key + " " + dic.Value;
    代码没测试,反正就这思路吧。
      

  7.   

     List<int> lis =new List<int>();
                Hashtable hash = new Hashtable();
                Random ra = new Random();
                int num = 6;
                int count = 0;
                for (int i = 0; i < num; i++)
                {
                    int inum = ra.Next(6);
                  // Console.WriteLine(inum);
                    lis.Add(inum);            }
                for (int i = 0; i < lis.Count;i++ )
                {
                    if (hash.ContainsValue(lis[i]))
                    {
                        ++count;
                        hash.Add(lis[i], count);
                    }
                    else { hash.Add(lis[i], 1); }
                }            foreach (DictionaryEntry dc in hash)
                 {
                     Console.WriteLine("\t" + dc.Key + "\t" + dc.Value);
                 }                Console.Read();
    这是我的代码,有点问题,不知道在哪?求指教
      

  8.   


                for (int i = 0; i < lis.Count;i++ )
                {
                    if (hash.ContainsValue(lis[i]))
                    {
                        ++count;
                        hash.Add(lis[i], count);
                    }
                    e
    你的count不是针对于某个随机数。
      

  9.   

    就一个dictionary,没有就add,有就对应的key的value++
      

  10.   

    居然没人用linq?
                int[] test = { 1, 3, 5, 3, 3, 1 };            var g = test.GroupBy(i => i);
                
                foreach (var key in g)
                {
                    Console.WriteLine("Number=" + key.Key + ", Count=" + key.Count());
                }
      

  11.   

    我给你一个调试后正确的代码吧,Hashtable:
    int[] arr = {1, 3, 5, 3, 3, 1};
                Hashtable hash = new Hashtable();
                int count = 0;
                for (int i = 0; i < arr.Length; ++i)
                {
                    if (hash.ContainsKey(arr[i]))
                    {
                        hash[arr[i]] = (int)hash[arr[i]] + 1;
                    }
                    else
                    {
                        count = 1;
                        hash.Add(arr[i], count);
                    }
                }
                foreach (DictionaryEntry dic in hash)
                    Console.WriteLine("值:" + dic.Key + ",出现次数:" + dic.Value);
                Console.Read();
    你可以学学19的,用Linq,这个我也不是很清楚,在学习中。