Advertisement
fbinnzhivko

02. Fun with Matrices

Mar 11th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Linq;
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         double start = double.Parse(Console.ReadLine());
  9.         double step = double.Parse(Console.ReadLine());
  10.  
  11.  
  12.         double[,] matrix = new double[4, 4];         //Fill matrix
  13.  
  14.         for (int row = 0; row < 4; row++)
  15.         {
  16.             for (int col = 0; col < 4; col++)
  17.             {
  18.                 matrix[row, col] = start;
  19.                 //Console.Write(matrix[row,col]+ " ");
  20.                 start = start + step;
  21.             }
  22.             //Console.WriteLine();
  23.         }
  24.         string[] command = Console.ReadLine().Split();
  25.  
  26.         while (command[0] != "Game")
  27.         {
  28.             int row = int.Parse(command[0]);
  29.             int col = int.Parse(command[1]);
  30.             string order = command[2];
  31.             double num = double.Parse(command[3]);
  32.  
  33.             switch (order)
  34.             {
  35.                 case "multiply": matrix[row, col] *= num; break;
  36.                 case "sum": matrix[row, col] += num; break;
  37.                 case "power": matrix[row, col] = Math.Pow(matrix[row, col], num); break;
  38.             }
  39.             command = Console.ReadLine().Split();
  40.         }
  41.         //for (int row = 0; row < 4; row++)
  42.         //{
  43.         //    for (int col = 0; col < 4; col++)
  44.         //    {
  45.         //        Console.Write(matrix[row, col] + " ");
  46.         //    }
  47.         //    Console.WriteLine();
  48.         //}
  49.         double maxSum = double.MinValue;
  50.         int index = 0;
  51.         string maxType = "ROW";
  52.  
  53.         for (int row = 0; row < 4; row++)
  54.         {
  55.             double sum = matrix[row, 0] + matrix[row, 1] + matrix[row, 2] + matrix[row, 3];
  56.             if (sum > maxSum)
  57.             {
  58.                 maxSum = sum;
  59.                 index = row;
  60.             }
  61.         }
  62.         for (int col = 0; col < 4; col++)
  63.         {
  64.             double sum = matrix[0, col] + matrix[1, col] + matrix[2, col] + matrix[3, col];
  65.             if (sum > maxSum)
  66.             {
  67.                 maxSum = sum;
  68.                 index = col;
  69.                 maxType = "COLUMN";
  70.             }
  71.  
  72.         }
  73.         double leftDiagonalSum = matrix[0, 0] + matrix[1, 1] + matrix[2, 2] + matrix[3, 3];
  74.  
  75.         if (leftDiagonalSum > maxSum)
  76.         {
  77.             maxSum = leftDiagonalSum;
  78.             maxType = "LEFT-DIAGONAL";
  79.         }
  80.         double rightDiagonalSum = matrix[3, 0] + matrix[2, 1] + matrix[1, 2] + matrix[0, 3];
  81.         if (rightDiagonalSum > maxSum)
  82.         {
  83.             maxSum = rightDiagonalSum;
  84.             maxType = "RIGHT-DIAGONAL";
  85.         }
  86.         if (maxType == "ROW" || maxType == "COLUMN")
  87.         {
  88.             Console.WriteLine("{0}[{1}] = {2:f2}", maxType, index, maxSum);
  89.         }
  90.         else
  91.         {
  92.             Console.WriteLine("{0} = {1:f2}", maxType,maxSum);
  93.         }
  94.        
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement