알고리즘 - 4. 최대값(MAX)
using System;
public class 최대값
{
public static void Main()
{
//[1] Initialize
int max = 0; // 해당 범위 내에서 가장 작은 값으로 초기화
//[2] Input
int[] data = { 2, 5, 3, 7, 1 };
//[3] Process : MAX
for (int i = 0; i < data.Length; i++)
{
if (data[i] > max)
{
max = data[i];
}
}
//[4] Output
Console.WriteLine("최대값 : {0}", max); // 7
//[5] Dispose
max = 0;
}
}
==============================================================================================
예제2 --> 배열 안의 값이 모두 "음의 정수"일 때 최대값 구하기
using System;
public class 최대값
{
public static void Main()
{
//[1] Initialize
int max = Int32.MinValue; // 정수형 데이터 중 작은 값으로 초기화
//[2] Input
int[] data = { -2, -5, -3, -7, -1 };
//[3] Process : MAX
for (int i = 0; i < data.Length; i++)
{
if (data[i] > max)
{
max = data[i];
}
}
//[4] Output
Console.WriteLine("최대값 : {0}", max); // -1
//[5] Dispose
max = 0;
}
}