美文网首页
c#找出下列整型数组中最大和最小值及其所在位置i。

c#找出下列整型数组中最大和最小值及其所在位置i。

作者: Class_Lee | 来源:发表于2018-01-04 10:37 被阅读0次

int[] a = { 2, -6, 9, 1, 3 };

int[] b = { -9, 5, 7, 3 };

int maxIndex1 = 0;

int minIndex1 = 0;

for (int i = 1; i < a.Length; i++) {

if (a [maxIndex1] < a [i]) {

maxIndex1 = i;

}

if (a [minIndex1] > a [i]) {

minIndex1 = i;

}

}

Console.WriteLine ("a数组最小值是:" + a [minIndex1]);

int maxIndex2 = 0;

int minIndex2 = 0;

for (int i = 1; i < b.Length; i++) {

if (b [maxIndex2] < b [i]) {

maxIndex2 = i;

}

if (b [minIndex2] > b [i]) {

minIndex2 = i;

}

}

Console.WriteLine ("b数组最小值是:" + b [minIndex2]);

// 求两数组最大值

int result1 = a [maxIndex1] > b [maxIndex2] ? a [maxIndex1] : a [maxIndex2];

Console.WriteLine ("最大值是:" + result1);

// 求两数组最小值

int result2 = a [minIndex1] < b [minIndex2] ? a [minIndex1] : b [minIndex2];

Console.WriteLine ("最小值是:" + result2);

相关文章

网友评论

      本文标题:c#找出下列整型数组中最大和最小值及其所在位置i。

      本文链接:https://www.haomeiwen.com/subject/haalnxtx.html