We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Project Euler #19: Counting Sundays
Project Euler #19: Counting Sundays
Sort by
recency
|
72 Discussions
|
Please Login in order to post a comment
I spent really enourmous time solving this one because of notorios test cases 3 and 4.
TLDR: use following snippet to read input values if you are using golang:
I made several optimizations, my code worked within milliseconds for all year ranges between 1900 and 10^16, but could not understand why my testcases fails because of timelimit. Then I added checks for input data, and only got wrong answer result. Finally I started adding checks for every line, crashing app immediately if check failed and putting it into forever cycle otherwise, thus getting the only diagnostic info info this platform allowed me and found out that I read empty dates. Then I tried several approaches (reading line and trimming spaces) but they did not work, looks like there are not only spaces, but linebreaks as well.
There is are a couple of very convenient features of calendars that many solutions here apear unaware of. Here's my solution.
//c#
using System;
public class Solution { private static int SUNDAY = 0;
public static void Main(string[] args) { int t = Convert.ToInt32(Console.ReadLine()); while (t-- > 0) { // input long[,] dateArray = new long[2, 3]; for (int i = 0; i < 2; i++) { string[] input = Console.ReadLine().Split(' '); for (int j = 0; j < 3; j++) { dateArray[i, j] = Convert.ToInt64(input[j]); } } // begin Adjust(ref dateArray); // long ak = 0; while (true) { if (dateArray[0, 2] == 1) { if (Verify(dateArray[0, 0], dateArray[0, 1], dateArray[0, 2])) { ak++; } } // set date dateArray[0, 2] = 1; // month plus 1 dateArray[0, 1]++; if (dateArray[0, 1] > 12) { dateArray[0, 1] = 1; dateArray[0, 0]++; } if (OverDate(dateArray)) { break; } } Console.WriteLine(ak); } }
/** * check if dateArray[0] is later than dateArray[1] * @param dateArray * @return */ private static bool OverDate(long[,] dateArray) { if (dateArray[0, 0] > dateArray[1, 0]) { return true; } else if (dateArray[0, 0] == dateArray[1, 0]) { if (dateArray[0, 1] > dateArray[1, 1]) { return true; } else if (dateArray[0, 1] == dateArray[1, 1]) { if (dateArray[0, 2] > dateArray[1, 2]) return true; } } return false; }
/** * let a[0] be earlier than a[1] * @param dateArray */ private static void Adjust(ref long[,] dateArray) { if (OverDate(dateArray)) { Exchange(ref dateArray); } }
/** * exchange two input date * @param dateArray */ private static void Exchange(ref long[,] dateArray) { long[] tmp = new long[3]; tmp = new long[3] { dateArray[1, 0], dateArray[1, 1], dateArray[1, 2] }; dateArray[1, 0] = dateArray[0, 0]; dateArray[1, 1] = dateArray[0, 1]; dateArray[1, 2] = dateArray[0, 2]; dateArray[0, 0] = tmp[0]; dateArray[0, 1] = tmp[1]; dateArray[0, 2] = tmp[2]; }
private static bool Verify(long year, long month, long day) { // w=(y + (y / 4) + (c / 4) - (2 * c) + ((26 * (month + 1)) / 10) + day - 1) % 7 // long sourceY = year; // long sourceM = month; if (month < 3) { month += 12; year -= 1; } long c = year / 100; long y = year % 100; int w = (int)((y + (y / 4) + (c / 4) - (2 * c) + ((26 * (month + 1)) / 10) + day - 1) % 7); bool t = w == SUNDAY; // if (t) { // Console.WriteLine(sourceY + "-" + sourceM + "-" + day); // } return t; } }
In c#:
using System;
public class Solution { private static int SUNDAY = 0;
}
100%