.
1. .
x n. i, + 1 1 n-1. max [0].
, max. max, max . .
. 1, . 1.
1 1
1
using System;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
const int n = 6;
int[] a = new int[n] { 3, 12, 5, -9, 8, -4 };
Console.WriteLine(" :");
for (int i = 0; i < n; ++i)
Console.WriteLine("\t" + a[i]);
Console.WriteLine();
int max = a[0];
for (int i = 0; i < n; ++i)
if (a[i] > max)
max = a[i];
Console.WriteLine(" = " + max);
Console.Read();
}
}
}
, .
:
2. .
:
x , ;
max ;
im ;
h ;
exch max .
.4 . , . .
, . , , , : .
2 2
, . 2.
2
using System;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
const int n = 6;
int ch;
int[] x = new int[n] { 3, 12, 5, -9, 8, -4 };
Console.WriteLine(" :");
|
|
for (int i = 0; i <= n - 1; ++i)
Console.Write(" " + x[i]);
int im = n - 1;
for (int j = 0; j <= n - 2; j++)
{
int max = x[j];
bool exch = false;
for (int i = j + 1; i <= n - 1; ++i)
if (x[i] > max)
{
max = x[i];
im = i;
exch = true;
}
if (exch == true)
{
ch = x[j];
x[j] = max;
x[im] = ch;
}
}
Console.WriteLine();
Console.WriteLine(" :");
for (int i = 0; i <= n - 1; ++i)
Console.Write(" " + x[i]);
Console.Read();
}
}
}
:
3. .
:
x , ;
n ;
i,j,k ;
ch .
. 3, . 3. . , . , , . . , n-1 , .. , .
, .
, . i . , , .
3 3
, i+1 , j .
( ) j , ch, .
3
using System;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
const int n = 6;
int ch;
int[] x = new int[n] { 3, 12, 5, -9, 8, -4 };
Console.WriteLine(" :");
for (int i = 0; i <= n - 1; ++i)
Console.Write(" " + x[i]);
for (int j = 1; j <= n - 1; j++)
{
ch = x[j];
int i = j - 1;
while ((i > -1) && (x[i] < ch))
i--;
for (int k = j - 1; k >= i+1; --k)
x[k + 1] = x[k];
x[i+1] = ch;
}
Console.WriteLine();
Console.WriteLine(" :");
for (int i = 0; i <= n - 1; ++i)
Console.Write(" " + x[i]);
Console.Read();
}
}
}
:
4. .
:
x , ;
n ;
|
|
i,j ;
ch ;
exch .
4
using System;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
const int n = 6;
int ch;
int[] x = new int[n] { 3, 12, 5, -9, 8, -4 };
Console.WriteLine(" :");
for (int i = 0; i <= n - 1; ++i)
Console.Write(" " + x[i]);
for (int j = 0; j <= n - 1; j++)
{
bool exch = false;
for (int i = 0; i <= n - 2 - j; ++i)
if (x[i] > x[i + 1])
{
ch = x[i + 1];
x[i + 1] = x[i];
x[i] = ch;
exch = true;
}
if (exch == false) break;
}
Console.WriteLine();
Console.WriteLine(" :");
for (int i = 0; i <= n - 1; ++i)
Console.Write(" " + x[i]);
Console.Read();
}
}
}
6 4
. , . n-1 .
( ). (exch=true), . (exch=false) break. . 4, . 4.
:
System.Arr
# rr, System. . 1.
1 - rr
Length | ( ) | |
Rank | ||
BinarySearch | ||
lear | ||
GetValue | ||
IndexOf | ||
LastIndexOf | ||
Reverse | ||
SetValue | ||
Sort |
Length , . . rr .
5 rr
using System;
namespace ConsoleApplication1
{
class Class1
{
static void Main()
{
int[] x = { 21, 2, 14, -6, 38, -7, 30};
Console.WriteLine();
Console.WriteLine(" :");
for (int i = 0; i < x.Length; ++i)
Console.Write(" " + x[i]);
Array.Sort(x);
Console.WriteLine();
Console.WriteLine(" :");
for (int i = 0; i < x.Length; ++i)
Console.Write(" " + x[i]);
Array.Reverse(x);
Console.WriteLine();
Console.WriteLine(" :");
for (int i = 0; i < x.Length; ++i)
Console.Write(" " + x[i]);
Console.Read();
}
}
}
|
|
Sort, Reverse , Array, , .
Sort . , , Reverse, , .
: