c#输入十个数,找出其中所有只出现过一次的数字。怎么样才能编辑一个可以从十个数中找出只出现拉一次的数字。
我先贴个简单的吧,后面写篇文章供大家参考,文章提供三种解法,对应不同的学习阶段。
Dictionary<int, int> input= new Dictionary<int, int>();
for(int i = 0; i < 10; i++)
{
Console.Write($"请输入第{i+1}个数:");
int temp = Convert.ToInt32(Console.ReadLine());
// 如果存在要添加的
if (input.ContainsKey(temp))
{
// 记录输入次数+1
input[temp]++;
}
else
{
// 不存在计数1次
input.Add(temp, 1);
}
}
Console.WriteLine($"出现过一次的有:");
foreach(var one in input)
{
if(one.Value == 1)
{
Console.WriteLine(one.Key);
}
}