这道题目的难点在于怎么在 C# 中存储一个超级长的字符串,并且有 合适的 format,比如说 排版 和换行,目前已知的方式是 :null,只能写长长的一行,值得注意的是,如果在字符串前面加上 @ 符号,则可以输出双引号等奇怪符号 “”
答案记录如下:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectEuler01
{
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Start();
// Write your code here!
const String p = @"73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557668966489504452445231617318564030987111217223831136222989342338030813533627661428280644448664523874930358907296290491560440772390713810515859307960866701724271218839987979087922749219016997208880937766572733300105336788122023542180975125454059475224352584907711670556013604839586446706324415722155397536978179778461740649551492908625693219784686224828397224137565705605749026140797296865241453510047482166370484403199890008895243450658541227588666881164271714799244429282308634656748139191231628245861786645835912456652947654568284891288314260769004207198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
int num_length = 13;
long largest = 0;
List<long> mynum = new List<long>();
for (int i = 0; i < p.Length-num_length; i++)
{
long numm = 1;
mynum.Clear();
numm = p[i]-'0';
mynum.Add(numm);
for (int j = 1; j < num_length; j++)
{
numm *= (p[i+j]-'0');
mynum.Add(p[i + j] - '0');
}
if (numm>largest)
{
largest = numm;
mynum.ForEach(Console.WriteLine);
Console.WriteLine("=========================");
}
}
Console.WriteLine(largest);
watch.Stop();
double ts = watch.Elapsed.TotalMilliseconds;
Console.WriteLine("Totoal running time is " + ts + " ms ");
Console.ReadLine();
}
}
}