n0throw

MathMemAns1

Nov 30th, 2021 (edited)
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. namespace ConsoleAppMathMem;
  2.  
  3. class Program
  4. {
  5.     private static readonly Random rand = new();
  6.     public static void Main()
  7.     {
  8.         Table table = new(rand.Next(2, 50 + 1));
  9.  
  10.         Console.WriteLine($"Size:\t{table.Size}");
  11.  
  12.         Console.WriteLine(table);
  13.  
  14.         Console.WriteLine($"Answer A:\t{table.GetA()}");
  15.         List<int> listB = table.GetB();
  16.         Console.WriteLine($"Answer B:\t{string.Join(' ', listB.Count == 0 ? "None" : listB)}");
  17.         Console.WriteLine($"Answer C:\t{table.GetC()}");
  18.     }
  19. }
  20.  
  21. public class Table
  22. {
  23.     private readonly Random rand = new();
  24.     private int[][] mass;
  25.     private int size;
  26.  
  27.     public int Size
  28.     {
  29.         get { return size; }
  30.         set
  31.         {
  32.             size = value;
  33.             mass = new int[size][];
  34.             for (int i = 0; i < size; i++)
  35.             {
  36.                 mass[i] = new int[size];
  37.                 for (int j = 0; j < size; j++)
  38.                 {
  39.                     if (i == j)
  40.                         mass[i][j] = 0;
  41.                     else
  42.                         mass[i][j] = rand.Next(0, 2 + 1);
  43.                 }
  44.             }
  45.         }
  46.     }
  47.     public Table(int size) => Size = size;
  48.  
  49.     public int GetA()
  50.     {
  51.         int output = 0;
  52.         for (int i = 0; i < size; i++)
  53.         {
  54.             if (mass[i].Count(num => num.Equals(0)) - 1
  55.                 < mass[i].Count(num => num.Equals(2)))
  56.             {
  57.                 output++;
  58.             }
  59.         }
  60.  
  61.         return output;
  62.     }
  63.  
  64.     public List<int> GetB()
  65.     {
  66.         List<int> output = new();
  67.  
  68.         for (int i = 0; i < size; i++)
  69.             if (mass[i].Count(num => num.Equals(0)) == 1)
  70.                 output.Add(i + 1);
  71.  
  72.         return output;
  73.     }
  74.  
  75.     public bool GetC()
  76.     {
  77.         int halfWin = (size + 1) / 2;
  78.  
  79.         for (int i = 0; i < size; i++)
  80.             if (mass[i].Count(num => num.Equals(2)) >= halfWin)
  81.                 return true;
  82.  
  83.         return false;
  84.     }
  85.  
  86.     public override string ToString()
  87.     {
  88.         string output = "";
  89.         for (var i = 0; i < size; i++)
  90.         {
  91.             for (var j = 0; j < size; j++)
  92.                 output += $"{mass[i][j]} ";
  93.             output += '\n';
  94.         }
  95.  
  96.         return output;
  97.     }
  98. }
Add Comment
Please, Sign In to add comment