.NET中 数组array1与数组array2,取出两个数组的交集的最大偶数和最小偶数
1个回答
展开全部
int[] array1 = new int[] { 23, 16, 4, 19, 22, 34, 3, 26 }; int[] array2 = new int[] { 19, 22, 3, 26, 23, 16, 4 };
List<int> interList = new List<int>();
foreach (int number in array1)
{
if ((number % 2 == 0) && (0 <= Array.IndexOf(array2, number)))
{
interList.Add(number);
}
}
if (interList.Count >= 2)
{
interList.Sort();
Console.WriteLine("交集最大偶数为:{0}, 最小偶数为:{1}", interList[interList.Count - 1], interList[0]);
}
else if (interList.Count == 1)
{
Console.WriteLine("交集最大偶数与最小偶数均为:{0}", interList[0]);
}
else
{
Console.WriteLine("两个数组无偶数交集");
}
List<int> interList = new List<int>();
foreach (int number in array1)
{
if ((number % 2 == 0) && (0 <= Array.IndexOf(array2, number)))
{
interList.Add(number);
}
}
if (interList.Count >= 2)
{
interList.Sort();
Console.WriteLine("交集最大偶数为:{0}, 最小偶数为:{1}", interList[interList.Count - 1], interList[0]);
}
else if (interList.Count == 1)
{
Console.WriteLine("交集最大偶数与最小偶数均为:{0}", interList[0]);
}
else
{
Console.WriteLine("两个数组无偶数交集");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询