Using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace NoNamespace { internal static class Program { private static void Main() { Console.WriteLine("Введите n и m"); var n = int.Parse(Console.ReadLine()); var m = int.Parse(Console.ReadLine()); if (n <= 0 || m <= 0) return;
var r = new Random(DateTime.Now.Millisecond); Console.WriteLine($"{nameof(Task1)}"); var a = Enumerable.Range(0, n).Select(i => r.Next(100)).ToArray(); Console.WriteLine(ArrayToString(a)); Console.WriteLine(ArrayToString(Task1(a))); Console.WriteLine();
Console.WriteLine($"{nameof(Task2)}"); var b = new int[n, m]; for (var i = 0; i < b.LongLength; i++) { b[i / n, i % n] = r.Next(-100, 100); } Console.WriteLine(MatrixToString(b)); Console.WriteLine(MatrixToString(Task2(b))); Console.WriteLine();
private static int[] Task1(IEnumerable<int> a) { return a.Where(x => x % 2 == 1).ToArray(); }
private static int[,] Task2(int[,] a) { var max = new Tuple<int, int>(0, 0); for (var i = 0; i < a.GetLength(1); i++) { for (var j = 0; j < a.GetLength(0); j++) { if (a[i, j] > a[max.Item1, max.Item2]) { max = new Tuple<int, int>(i, j); } } }
Answers & Comments
Verified answer
Using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NoNamespace
{
internal static class Program
{
private static void Main()
{
Console.WriteLine("Введите n и m");
var n = int.Parse(Console.ReadLine());
var m = int.Parse(Console.ReadLine());
if (n <= 0 || m <= 0)
return;
var r = new Random(DateTime.Now.Millisecond);
Console.WriteLine($"{nameof(Task1)}");
var a = Enumerable.Range(0, n).Select(i => r.Next(100)).ToArray();
Console.WriteLine(ArrayToString(a));
Console.WriteLine(ArrayToString(Task1(a)));
Console.WriteLine();
Console.WriteLine($"{nameof(Task2)}");
var b = new int[n, m];
for (var i = 0; i < b.LongLength; i++)
{
b[i / n, i % n] = r.Next(-100, 100);
}
Console.WriteLine(MatrixToString(b));
Console.WriteLine(MatrixToString(Task2(b)));
Console.WriteLine();
Console.WriteLine($"{nameof(Task3)}");
Console.WriteLine(MatrixToString(b));
Console.WriteLine(ArrayToString(Task3(b)));
Console.WriteLine();
Console.ReadLine();
}
private static int[] Task1(IEnumerable<int> a)
{
return a.Where(x => x % 2 == 1).ToArray();
}
private static int[,] Task2(int[,] a)
{
var max = new Tuple<int, int>(0, 0);
for (var i = 0; i < a.GetLength(1); i++)
{
for (var j = 0; j < a.GetLength(0); j++)
{
if (a[i, j] > a[max.Item1, max.Item2])
{
max = new Tuple<int, int>(i, j);
}
}
}
var maxv = a[max.Item1, max.Item2];
a[max.Item1, max.Item2] = a[0, 0];
a[0, 0] = maxv;
return a;
}
private static int[] Task3(int[,] a)
{
var list = new List<int>();
for (var i = 0; i < a.GetLength(1); i++)
{
for (var j = 0; j < a.GetLength(0); j++)
{
if (a[i, j] < 0 && Math.Abs(a[i, j] % 2) == 1)
{
list.Add(a[i, j]);
}
}
}
return list.ToArray();
}
private static string ArrayToString(int[] a)
{
if (!a.Any())
return "Таких элементов нет";
var sb = new StringBuilder("Массив: ");
foreach (var ae in a)
{
sb.Append($"{ae} ");
}
return sb.ToString();
}
private static string MatrixToString(int[,] a)
{
if (a.LongLength == 0)
return "Таких элементов нет";
var sb = new StringBuilder("Матрица: \n");
for (var i = 0; i < a.GetLength(1); i++)
{
for (var j = 0; j < a.GetLength(0); j++)
{
sb.Append($"{a[i, j]} ");
}
sb.AppendLine();
}
return sb.ToString();
}
}
}