Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
37
Task/Five-weekends/C-sharp/five-weekends-1.cs
Normal file
37
Task/Five-weekends/C-sharp/five-weekends-1.cs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
using System;
|
||||
|
||||
namespace _5_Weekends
|
||||
{
|
||||
class Program
|
||||
{
|
||||
const int FIRST_YEAR = 1900;
|
||||
const int LAST_YEAR = 2100;
|
||||
static int[] _31_MONTHS = { 1, 3, 5, 7, 8, 10, 12 };
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
int totalNum = 0;
|
||||
int totalNo5Weekends = 0;
|
||||
|
||||
for (int year = FIRST_YEAR; year <= LAST_YEAR; year++)
|
||||
{
|
||||
bool has5Weekends = false;
|
||||
|
||||
foreach (int month in _31_MONTHS)
|
||||
{
|
||||
DateTime firstDay = new DateTime(year, month, 1);
|
||||
if (firstDay.DayOfWeek == DayOfWeek.Friday)
|
||||
{
|
||||
totalNum++;
|
||||
has5Weekends = true;
|
||||
Console.WriteLine(firstDay.ToString("yyyy - MMMM"));
|
||||
}
|
||||
}
|
||||
|
||||
if (!has5Weekends) totalNo5Weekends++;
|
||||
}
|
||||
Console.WriteLine("Total 5-weekend months between {0} and {1}: {2}", FIRST_YEAR, LAST_YEAR, totalNum);
|
||||
Console.WriteLine("Total number of years with no 5-weekend months {0}", totalNo5Weekends);
|
||||
}
|
||||
}
|
||||
}
|
||||
37
Task/Five-weekends/C-sharp/five-weekends-2.cs
Normal file
37
Task/Five-weekends/C-sharp/five-weekends-2.cs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
const int startYear = 1900, endYear = 2100;
|
||||
|
||||
var query = (
|
||||
from year in startYear.To(endYear)
|
||||
from month in 1.To(12)
|
||||
where DateTime.DaysInMonth(year, month) == 31
|
||||
select new DateTime(year, month, 1) into date
|
||||
where date.DayOfWeek == DayOfWeek.Friday
|
||||
select date)
|
||||
.ToList();
|
||||
|
||||
Console.WriteLine("Count: " + query.Count);
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("First and last 5:");
|
||||
for (int i = 0; i < 5; i++)
|
||||
Console.WriteLine(query[i].ToString("MMMM yyyy"));
|
||||
Console.WriteLine("...");
|
||||
for (int i = query.Count - 5; i < query.Count; i++)
|
||||
Console.WriteLine(query[i].ToString("MMMM yyyy"));
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Years without 5 weekends:");
|
||||
Console.WriteLine(string.Join(" ", startYear.To(endYear).Except(query.Select(dt => dt.Year))));
|
||||
}
|
||||
}
|
||||
|
||||
public static class IntExtensions
|
||||
{
|
||||
public static IEnumerable<int> To(this int start, int end) => Enumerable.Range(start, end - start + 1);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue