Advertisement
Guest User

FunWithMatricesCorrect

a guest
Mar 27th, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 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 = 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":
  34.                     matrix[row, col] *= num;
  35.                     break;
  36.                 case "sum":
  37.                     matrix[row, col] += num;
  38.                     break;
  39.                 case "power":
  40.                     matrix[row, col] = Math.Pow(matrix[row, col], num);
  41.                     break;
  42.             }
  43.  
  44.             command = Console.ReadLine().Split();
  45.         }
  46.  
  47.         double maxSum = double.MinValue;
  48.         int index = 0;
  49.         string maxType = "ROW";
  50.  
  51.         for (int row = 0; row < 4; row++)
  52.         {
  53.             double sum = matrix[row, 0] + matrix[row, 1] + matrix[row, 2] + matrix[row, 3];
  54.             if (sum > maxSum)
  55.             {
  56.                 maxSum = sum;
  57.                 index = row;
  58.             }
  59.         }
  60.  
  61.         for (int col = 0; col < 4; col++)
  62.         {
  63.             double sum = matrix[0, col] + matrix[1, col] + matrix[2, col] + matrix[3, col];
  64.             if (sum > maxSum)
  65.             {
  66.                 maxSum = sum;
  67.                 index = col;
  68.                 maxType = "COLUMN";
  69.             }
  70.         }
  71.  
  72.         double leftDiagonalSum = matrix[0, 0] + matrix[1, 1] + matrix[2, 2] + matrix[3, 3];
  73.         if (leftDiagonalSum > maxSum)
  74.         {
  75.             maxSum = leftDiagonalSum;
  76.             maxType = "LEFT-DIAGONAL";
  77.         }
  78.  
  79.         double rightDiagonalSum = matrix[3, 0] + matrix[2, 1] + matrix[1, 2] + matrix[0, 3];
  80.         if (rightDiagonalSum > maxSum)
  81.         {
  82.             maxSum = rightDiagonalSum;
  83.             maxType = "RIGHT-DIAGONAL";
  84.         }
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement