Guest User

FunWithMatrices1

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