我只会这样写. 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();
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); }
你随机生成“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; 代码没测试,反正就这思路吧。
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(); 这是我的代码,有点问题,不知道在哪?求指教
for (int i = 0; i < lis.Count;i++ ) { if (hash.ContainsValue(lis[i])) { ++count; hash.Add(lis[i], count); } e 你的count不是针对于某个随机数。
就一个dictionary,没有就add,有就对应的key的value++
居然没人用linq? int[] test = { 1, 3, 5, 3, 3, 1 }; var g = test.GroupBy(i => i);
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();
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);
}
请问能用hashtable实现吗?
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;
代码没测试,反正就这思路吧。
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();
这是我的代码,有点问题,不知道在哪?求指教
for (int i = 0; i < lis.Count;i++ )
{
if (hash.ContainsValue(lis[i]))
{
++count;
hash.Add(lis[i], count);
}
e
你的count不是针对于某个随机数。
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());
}
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,这个我也不是很清楚,在学习中。