Project Euler 8

Problem 8

  • char.IsDigit(c) で文字 c が数値かどうか判定
  • 文字 c から数値への変換は、c - '0' で出来た
using System;
using System.IO;
using System.Collections.Generic;

namespace PE {
    public class PE008 {
        public static void Calc() {
            string text = File.ReadAllText("./data/008.txt");

            var ds = new List<int>();
            foreach (char c in text) {
                if (char.IsDigit(c)) {
                    ds.Add(c - '0');
                }
            }

            int ans = 0;
            for (int i = 4; i < ds.Count; i++) {
                int x = 1;
                for (int j = i-4; j <= i; j++) {
                    x *= ds[j];
                }
                ans = Math.Max(ans, x);
            }
            Console.WriteLine(ans);
        }
    }
}