.


:




:

































 

 

 

 


foreach

. , , .

. , # .

. , , . n*m, :

int[,]_ = new int[n,m];

, [,] .

, , , . , , i j, , :

_[i,j] = a;

, . :

[,] _ = {

{val00, val01, val02, , val0m}

{val10, val11 val12, , val1m}

{valn0, valn1, valn2, ,valnm}

};

valij . . , ..

.

1) , . :

[,] ;

:

int[,] ;

2) , new 0. :

[,] = new [ _1, _2 ];

:

int[,] b = newint [2, 3];

3) , , :

[,] = new [,] { _ };

:

int[,] c = newint[,]] {{1,2,3),{4,5,6}};

int, .

4) new , , , :

[,] = { _ };

:

int[,] = {{1,2,3},{4,5,6}};

5) . , .

[,] = new [ 1, 2]{ _};

:

int[,] d = newint[2,3]] {{1,2,3),{4,5,6}}

, , , . , , , :

[1,4] b[i, j] b[j, i]

, , , , .

1. m*n (. 1).

1 m n

. . 2, 1.

1- 1

usingSystem;

namespace ConsoleApplication1

{

classClass1

{

staticvoid Main()

{ constint m = 3, n = 4;

int[,] a = newint[m, n] {

{ 2, 2, 8, 9 },

{ 4, 5, 6, 2 },

{ 7, 0, 1, 1 }

};

Console.WriteLine(":");

for (inti = 0; i< m; ++i)

{

for (int j = 0; j < n; ++j)

Console.Write(" " + a[i, j]);

Console.WriteLine();

}

for (inti = 0; i< m; ++i)

{

int sum = 0;

for (int j = 0; j < n; ++j)

sum += a[i, j];

Console.WriteLine(" {0}{1}",i, sum);

}

Console.Read();

}

}

}

 

2 1

:

 

, sum , .

2. .

. 3, . 2.

i, +1 0 m-1 j, +1 0 n-1.

max [0,0] . j max.

 

 
 

 

 


3 2

, max . i j, i .

2 2

usingSystem;

namespace ConsoleApplication1

{

classProgram

{

staticvoid Main(string[] args)

{

constint m = 3, n = 4;

int[,] y = newint[m, n] {

{ 2, 2, 8, 9 },

{ 4, 5, 6, 2 },

{ 7, 0, 1, 1 }

};

 

Console.WriteLine(":");

for (inti = 0; i< m; ++i)

{

for (int j = 0; j < n; ++j)

Console.Write(" " + y[i, j]);

Console.WriteLine();

}

int max=y[0,0];

for (inti = 0; i< m; ++i)

{

for (int j = 0; j < n; ++j)

if (y[i, j] > max) max = y[i, j];

}

Console.WriteLine(" "+ max);

}

}

}

:

 

Random

Random, System, , , . , . , , , .

# Random: int. .

:

Randoma = newRandom();

, , .

int:

Randomb = newRandom(5);

, .

, . 1.

1 System.Random

Next() int
Next() [0, ]
Next(, ) [, ]
NextDouble() [0. 1)
NextBytes() [0, 255]

. . . 1 .

, (.3).

3

usingSystem;

namespaceConsoleApplication1

{ classProgram

{Random l = newRandom();

Random d = newRandom(1);

constint n = 5;

Console.WriteLine(" [0, 1]");

for (inti = 0; i< n; ++i)

Console.Write("{0,6:0.##}", l.NextDouble());

Console.WriteLine();

Console.WriteLine(" [0, 100]");

for (inti = 0; i< n; ++i)

Console.Write(" " + d.Next(100));

Console.WriteLine();

Console.WriteLine(" [-5, 5]");

for (inti = 0; i< n; ++i)

Console.Write(" " + l.Next(-5, 5));

Console.WriteLine();

Console.WriteLine("[0, 255]");

byte[ ] mas = newbyte[n];

l.NextBytes(mas);

for (inti = 0; i< n; ++i)

Console.Write(" " + mas[i]);

}

}

}

:

# , .

4

, . . , : , . , . . 4.

:

[ ] [ ] ;

, :

int[][] b = newint [3][];

, , . , , , , :

0- (5 ):

b[0]=newint [5]; //

1- (3 ):

b[1] =newint [3];

2- (4 ):

b[2] = newint [4];

b[0 ], b[1] b[2] , , (. 3).

:

int[] [] b = {newint[5], newint[3], newint[4]};

K , , :

b[1][2] b[i][j] b[j][i]

.

4 rr .

4 - rr

using System;

namespace ConsoleApplication3

{

classProgram

{

staticvoid Main(string[] args)

{

int[][] b = newint[3][];

b[0] = newint[5] { 4, 5, 8, 3, 6 };

b[1] = newint[3] { 7, 9, 1 };

b[2] = newint[4] { 6, 5, 3, 1 };

 

Console.WriteLine(":");

for (inti = 0; i<b.Length; ++i)

{

for (int j = 1; j <b[i].Length; ++j)

Console.Write(" " + b[i][j]);

Console.WriteLine();

}

Console.WriteLine(Array.IndexOf(b[0], 8));

 

}

}

}

, ( ).

:

foreach

foreach . # , , .

foreach:

foreach ( _ in ) ;

_ , foreach , .

, , . , , , .

.

5 - foreach

using System;

namespace ConsoleApplication5

{

classProgram

{

staticvoid Main(string[] args)

{

constint n = 6;

int[] a = newint[n] { 3, 12, 5, -9, 8, -4 };

 

Console.WriteLine(":");

for (inti = 0; i< n; ++i)

Console.WriteLine("\t" + a[i]);

Console.WriteLine();

int max = a[0];

foreach (int x in a)

{

if (x > max)

max = x;

}

Console.WriteLine(" = " + max);

Console.Read();

}

}

}

foreach . if max.

:

, , , , . , break.

foreach .

6 - foreach

using System;

namespace ConsoleApplication1

{

classProgram

{

staticvoid Main(string[] args)

{

constint m = 3, n = 4;

int[,] y = newint[m, n] {

{ 2, 2, 8, 9 },

{ 4, 5, 6, 2 },

{ 7, 0, 1, 1 }

};

 

Console.WriteLine(":");

for (inti = 0; i< m; ++i)

{

for (int j = 0; j < n; ++j)

Console.Write(" " + y[i, j]);

Console.WriteLine();

}

int max = y[0, 0];

foreach (int x in y){

if (x > max) max = x;

}

Console.WriteLine(" " + max);

Console.Read();

}

}

}

foreach for .

:

 

. , . , . , .

# , . :

[,,] = new [ 1,N];

, :

int [,,] b= newint[5,4,3];

, :

b [2,1,2]

, .

(. 7) .

7 -

using System;

namespace ConsoleApplication1

{

classProgram

{

staticvoid Main(string[] args)

{

constint m = 3, n = 4, c=2;

int[,,] y = newint[c,m, n] {{

{ 2, 2, 8, 9 },

{ 4, 5, 6, 2 },

{ 7, 0, 1, 1 }},

{

{ 1, 1, 0, 1 },

{ 4, 5, 6, 2 },

{ 7, 0, 1, 1 }}};

Console.WriteLine(":");

for (int k = 0; k < c; ++k)

{

for (inti = 0; i< m; ++i)

{

for (int j = 0; j < n; ++j)

Console.Write(" " + y[k, i, j]);

Console.WriteLine();

}

}

int max = y[0, 0,0];

for (int k = 0; k < c; ++k)

{

for (inti = 0; i< m; ++i)

{

for (int j = 0; j < n; ++j)

if (y[k, i, j] > max) max = y[k, i, j];

}

}

Console.WriteLine(" " + max);

}

}

}

 

( ), m n .

, : , , , .

 

:

 



<== | ==>
 |
:


: 2017-02-28; !; : 532 |


:

:

, , .
==> ...

1601 - | 1268 -


© 2015-2024 lektsii.org - -

: 0.136 .