如何把两个int数组中差最小的两个元素拿出来,组成一个二维数组,在线等!!!!!
如何把两个int数组中差最小的两个元素拿出来,组成一个二维数组,在线等!!!!!
例 a: 1,7,8,10,50,58,59,60
b: 4,5,7,8,9,20,50,60
------解决方案--------------------static void Main(string[] args)
{
int[] a=new int[]{ 1,7,8,10,50,58,59,60};
int[] b = new int[] { 4, 5, 7, 8, 9, 20, 50, 60 };
foreach (int[] var in Get(a,b))
{
Console.WriteLine(var[0]+ "- "+var[1]);
}
}
static int[][] Get(int[] a, int[] b)
{
int min = int.MaxValue;
Dictionary <int, List <int[]> > result = new Dictionary <int, List <int[]> > ();
for (int i = 0; i < a.Length; i++)
{
int diff = Math.Abs(a[i] - b[i]);
if (diff < min)
{
min = diff;
};
if (!result.ContainsKey(diff))
{
result.Add(diff, new List <int[]> ());
}
result[diff].Add(new int[] { a[i], b[i] });
}
return result[min].ToArray();
}
------解决方案-------------------- int[] a=new int[]{ 1,7,8,10,50,58,59,60};
int[] b = new int[] { 4, 5, 7, 8, 9, 20, 50, 60 };
int min1=99;
int min2=99;
for (int i=0;i <a.Length ;i++)
{
if (a[i] <min1)
min1=a[i];
}
for (int i=0;i <b.Length ;i++)
{
if (a[i] <min2)
min2=a[i];
}
int[,] C=new int[min1,min1];
------解决方案-------------------- int[] a=new int[]{ 1,7,8,10,50,58,59,60};
int[] b = new int[] { 4, 5, 7, 8, 9, 20, 50, 60 };
int min1=99;
int min2=99;
for (int i=0;i <a.Length ;i++)
{
if (a[i] <min1)
min1=a[i];
}
for (int i=0;i <b.Length ;i++)
{
if (b[i] <min2)
min2=b[i];
}
int[,] C=new int[min1,min2];