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
Contest ends in
Sort by
recency
|
70 Discussions
|
Please Login in order to post a comment
//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++
Approach: figure out what day is January 1st for the start year.
This can be done mathematically by adding 365 starting from year 1900, plus the number of leap years before the end year.
From January 1st, we go to the first of each months, counting Sundays. We need to do it no more than 12 * 1000 times.
C++