c#找出下列整型數(shù)組中最大和最小值及其所在位置i。

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數(shù)組最小值是:" + 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數(shù)組最小值是:" + b [minIndex2]);

// 求兩數(shù)組最大值

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

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

// 求兩數(shù)組最小值

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

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

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容