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
|
71 Discussions
|
Please Login in order to post a comment
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%
My code failed for test cases 2 3 4 5 but i didnt really what's wrong with my code Can someone help me, my code is written in C++