|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +public static class Program |
| 6 | +{ |
| 7 | + private static void ShowUsage() |
| 8 | + { |
| 9 | + Console.Error.WriteLine("Usage: please enter the dimension of the matrix and the serialized matrix"); |
| 10 | + Environment.Exit(1); |
| 11 | + } |
| 12 | + |
| 13 | + private static List<int> ParseIntegerList(string input) |
| 14 | + { |
| 15 | + if (string.IsNullOrWhiteSpace(input)) |
| 16 | + ShowUsage(); |
| 17 | + |
| 18 | + var tokens = input |
| 19 | + .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); |
| 20 | + |
| 21 | + var numbers = new List<int>(); |
| 22 | + foreach (var token in tokens) |
| 23 | + { |
| 24 | + if (!int.TryParse(token, out int value)) |
| 25 | + ShowUsage(); |
| 26 | + |
| 27 | + numbers.Add(value); |
| 28 | + } |
| 29 | + |
| 30 | + return numbers; |
| 31 | + } |
| 32 | + |
| 33 | + static List<int> TransposeMatrix(int cols, int rows, List<int> input) |
| 34 | + { |
| 35 | + var result = new List<int>(new int[rows * cols]); |
| 36 | + |
| 37 | + for (int i = 0; i < rows; ++i) |
| 38 | + { |
| 39 | + for (int j = 0; j < cols; ++j) |
| 40 | + { |
| 41 | + int index = j * rows + i; |
| 42 | + result[index] = input[i * cols + j]; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return result; |
| 47 | + } |
| 48 | + |
| 49 | + static int Main(string[] args) |
| 50 | + { |
| 51 | + if (args.Length != 3) |
| 52 | + ShowUsage(); |
| 53 | + |
| 54 | + if (!int.TryParse(args[0], out var cols)) |
| 55 | + ShowUsage(); |
| 56 | + |
| 57 | + if (!int.TryParse(args[1], out var rows)) |
| 58 | + ShowUsage(); |
| 59 | + |
| 60 | + var numbers = ParseIntegerList(args[2]); |
| 61 | + if (numbers.Count != cols * rows) |
| 62 | + ShowUsage(); |
| 63 | + |
| 64 | + var transposed = TransposeMatrix(cols, rows, numbers); |
| 65 | + Console.WriteLine(string.Join(", ", transposed)); |
| 66 | + return 0; |
| 67 | + } |
| 68 | +} |
0 commit comments