Problem 11
- C# ポケットリファレンス を読みながら、ファイル読み込みや、文字列処理を書いています。
- File.ReadAllText, File.ReadAllLinesで一括でテキストを読み込む
- String.Split で文字列を分割
int best = 0
を var best = 0
と書いたら内部ではどう扱われるのだろう
using System;
using System.IO;
using System.Collections.Generic;
public class P011 {
static int calc(int row, int col, int drow, int dcol, List<List<int>> table) {
int rows = table.Count;
int cols = table[0].Count;
int r = row;
int c = col;
int ret = 1;
for (int i = 0; i < 4; i++) {
if (0 <= r && r < rows && 0 <= c && c < cols) {
ret *= table[r][c];
r += drow;
c += dcol;
}
else return 0;
}
return ret;
}
public static void Main() {
var lines = File.ReadAllLines("../11.txt");
var table = new List<List<int>>();
foreach (string s in lines) {
var xs = new List<int>();
foreach (var t in s.Split(' ')) {
xs.Add(int.Parse(t));
}
table.Add(xs);
}
int rows = table.Count;
int cols = table[0].Count;
int best = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
best = Math.Max(best, calc(i, j, 0, 1, table));
best = Math.Max(best, calc(i, j, 1, 0, table));
best = Math.Max(best, calc(i, j, 1, 1, table));
best = Math.Max(best, calc(i, j, 1, -1, table));
}
}
Console.WriteLine(best);
}
}