程式碼示例:
using System;
using System.Collections.Generic;
using System.Text;
namespace test
{
class Program
static void Main(string[] args)
int n,max,min,max_index,min_index;
Console.WriteLine("請輸入陣列的長度:");
n=Convert.ToInt32(Console.ReadLine());
int[] a = new int[n];
for (int i = 0; i < n; i++)
Console.WriteLine("請輸入第{0}個數的值:", i + 1);
a[i] = Convert.ToInt32(Console.ReadLine());
}
max = a[0];
max_index = 0;
min = a[0];
min_index = 0;
for (int i = 1; i < n; i++)
if (a[i] > max)
max = a[i];
max_index = i;
if (a[i] < min)
min = a[i];
min_index = i;
Console.WriteLine("最大的值是:{0},索引是:{1} (從0開始編號)", max, max_index);
Console.WriteLine("最小的值是:{0},索引是:{1} (從0開始編號)", min, min_index);
Console.Read();
程式碼示例:
using System;
using System.Collections.Generic;
using System.Text;
namespace test
{
class Program
{
static void Main(string[] args)
{
int n,max,min,max_index,min_index;
Console.WriteLine("請輸入陣列的長度:");
n=Convert.ToInt32(Console.ReadLine());
int[] a = new int[n];
for (int i = 0; i < n; i++)
{
Console.WriteLine("請輸入第{0}個數的值:", i + 1);
a[i] = Convert.ToInt32(Console.ReadLine());
}
max = a[0];
max_index = 0;
min = a[0];
min_index = 0;
for (int i = 1; i < n; i++)
{
if (a[i] > max)
{
max = a[i];
max_index = i;
}
if (a[i] < min)
{
min = a[i];
min_index = i;
}
}
Console.WriteLine("最大的值是:{0},索引是:{1} (從0開始編號)", max, max_index);
Console.WriteLine("最小的值是:{0},索引是:{1} (從0開始編號)", min, min_index);
Console.Read();
}
}
}