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(@"Task1"); var a = new int[n].ChangeEach(x => r.Next(100)); Console.WriteLine(a.Print()); Console.WriteLine(a.Where(x => x%2 == 1).Print()); Console.WriteLine();
Console.WriteLine(@"Task2"); var b = new int[n, m].ChangeEach(x => r.Next(-100, 100)); Console.WriteLine(b.Print()); Console.WriteLine(Task2(b).Print()); Console.WriteLine();
private static int[,] Task2(int[,] a) { var max = a.MaxIndex((x, y) => x > y); var maxv = a[max.Item1, max.Item2]; a[max.Item1, max.Item2] = a[0, 0]; a[0, 0] = maxv;
return a; } }
public static class Extensions { public static List<T> Where<T>(this T[,] array, Func<T, bool> filter) { if (filter == null) return null;
var list = new List<T>();
for (var i = 0; i < array.GetLength(1); i++) for (var j = 0; j < array.GetLength(0); j++) if (filter(array[i, j])) list.Add(array[i, j]);
return list; }
public static string Print<T>(this IEnumerable<T> a) { var enumerable = a as IList<T> ?? a.ToList(); if (!enumerable.Any()) return "Таких элементов нет";
var sb = new StringBuilder("Массив: "); foreach (var ae in enumerable) sb.Append($"{ae} ");
return sb.ToString(); }
public static T[,] ChangeEach<T>(this T[,] array, Func<T, T> mutator) { if (mutator == null) return array;
for (var i = 0; i < array.GetLength(1); i++) for (var j = 0; j < array.GetLength(0); j++) array[i, j] = mutator(array[i, j]); return array; }
public static T[] ChangeEach<T>(this T[] array, Func<T, T> mutator) { if (mutator == null) return array;
public static string Print<T>(this T[,] 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(); }
public static Tuple<int, int> MaxIndex<T>(this T[,] array, Func<T, T, bool> comparer) { if (comparer == null) return new Tuple<int, int>(-1, -1);
var index = new Tuple<int, int>(0, 0);
for (var i = 0; i < array.GetLength(1); i++) for (var j = 0; j < array.GetLength(0); j++) if (comparer(array[i, j], array[index.Item1, index.Item2])) index = 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(@"Task1");
var a = new int[n].ChangeEach(x => r.Next(100));
Console.WriteLine(a.Print());
Console.WriteLine(a.Where(x => x%2 == 1).Print());
Console.WriteLine();
Console.WriteLine(@"Task2");
var b = new int[n, m].ChangeEach(x => r.Next(-100, 100));
Console.WriteLine(b.Print());
Console.WriteLine(Task2(b).Print());
Console.WriteLine();
Console.WriteLine(@"Task3");
Console.WriteLine(b.Print());
Console.WriteLine(b.Where(x => (x < 0) && (Math.Abs(x%2) == 1)).Print());
Console.WriteLine();
Console.ReadLine();
}
private static int[,] Task2(int[,] a)
{
var max = a.MaxIndex((x, y) => x > y);
var maxv = a[max.Item1, max.Item2];
a[max.Item1, max.Item2] = a[0, 0];
a[0, 0] = maxv;
return a;
}
}
public static class Extensions
{
public static List<T> Where<T>(this T[,] array, Func<T, bool> filter)
{
if (filter == null)
return null;
var list = new List<T>();
for (var i = 0; i < array.GetLength(1); i++)
for (var j = 0; j < array.GetLength(0); j++)
if (filter(array[i, j]))
list.Add(array[i, j]);
return list;
}
public static string Print<T>(this IEnumerable<T> a)
{
var enumerable = a as IList<T> ?? a.ToList();
if (!enumerable.Any())
return "Таких элементов нет";
var sb = new StringBuilder("Массив: ");
foreach (var ae in enumerable)
sb.Append($"{ae} ");
return sb.ToString();
}
public static T[,] ChangeEach<T>(this T[,] array, Func<T, T> mutator)
{
if (mutator == null)
return array;
for (var i = 0; i < array.GetLength(1); i++)
for (var j = 0; j < array.GetLength(0); j++)
array[i, j] = mutator(array[i, j]);
return array;
}
public static T[] ChangeEach<T>(this T[] array, Func<T, T> mutator)
{
if (mutator == null)
return array;
for (var j = 0; j < array.GetLength(0); j++)
array[j] = mutator(array[j]);
return array;
}
public static string Print<T>(this T[,] 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();
}
public static Tuple<int, int> MaxIndex<T>(this T[,] array, Func<T, T, bool> comparer)
{
if (comparer == null)
return new Tuple<int, int>(-1, -1);
var index = new Tuple<int, int>(0, 0);
for (var i = 0; i < array.GetLength(1); i++)
for (var j = 0; j < array.GetLength(0); j++)
if (comparer(array[i, j], array[index.Item1, index.Item2]))
index = new Tuple<int, int>(i, j);
return index;
}
}
}