Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
project euler 11, C#
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { string s_grid = Console.In.ReadToEnd(); string[] n = s_grid.Split(" \n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); int size = 20; int window = 4; List<List<int>> grid = new List<List<int>>(); int counter = 0; foreach(var s in n) { if(counter==size) break; var row = new List<int>(); for(int i=0; i<size; i++) { row.Add(Convert.ToInt32(n[counter*size+i])); } grid.Add(row); counter++; } int max_hor = Max(grid, window); int max_ver = Max(Transpose(grid), window); int max_diag = Max(RowsFromDiagonals(grid), window); Console.WriteLine(new List<int>(){max_hor, max_ver, max_diag}.Max()); } public static int Max(List<List<int>> grid, int window) { int max = 0; foreach(var row in grid) { for(int i=0; i<row.Count(); i++) { int product = 1; for(int j=i; j<i+window; j++) { if(row.Count() <= j) break; product *= row[j]; if(product > max) max = product; } } } return max; } public static List<List<int>> Transpose(List<List<int>> grid) { var res = new List<List<int>>(); for(int column=0; column<grid.Count(); column++) { List<int> row_t = new List<int>(); foreach(var row in grid) { row_t.Add(row[column]); } res.Add(row_t); } return res; } public static List<List<int>> RowsFromDiagonals(List<List<int>> grid) { var res = new List<List<int>>(); for(int i=0; i<grid[0].Count(); i++) { List<int> row = new List<int>(); for(int j=0, k=i; ; j++, k++) { if(grid.Count() <= j || grid.Count() <= k) break; row.Add(grid[j][k]); } res.Add(row); } var grid_t = Transpose(grid); for(int i=0; i<grid_t[0].Count(); i++) { List<int> row = new List<int>(); for(int j=0, k=i; ; j++, k++) { if(grid_t.Count() <= j || grid_t.Count() <= k) break; row.Add(grid_t[j][k]); } res.Add(row); } //the "other" diagonals for(int i=grid[0].Count()-1; i>=0; i--) { List<int> row = new List<int>(); for(int j=0, k=i; ; j++, k--) { if(grid.Count() <= j || k < 0) break; row.Add(grid[j][k]); } res.Add(row); } //... return res; } } }
run
|
edit
|
history
|
help
0
C:\Documents and Settings\Админ\Рабочий стол\ШЭ информатика
abcd
Bb
FizzBuzz Solution
Cotton Farm v: (0.0.0.002)
Intuit // C# // listing_4.5 (Math. /Pow /Sin)
adcsasxsd
C# Extension Method
4/03/2017
1