周五13ths【难度:2级】:
答案1:
using System;
using System. Collections. Generic;
public static class Kata
{
public static string FridayTheThirteenths ( int Start, int End = int . MinValue)
{
if ( End== int . MinValue) End = Start;
List< string > theThirteenths = new List < string > ( ) ;
for ( int year = Start; year<= End; year++ )
{
for ( int month = 1 ; month<= 12 ; month++ )
{
DateTime date = new DateTime ( year, month, 13 ) ;
if ( date. DayOfWeek== DayOfWeek. Friday)
theThirteenths. Add ( date. ToString ( "M/dd/yyyy" ) ) ;
}
}
return String. Join ( " " , theThirteenths) ;
}
}
答案2:
using System;
using System. Globalization;
using System. Linq;
public static class Kata
{
public static string FridayTheThirteenths ( int startYear, int endYear = int . MinValue) = >
string . Join ( " " , Enumerable. Range ( startYear, endYear == int . MinValue ? 1 : endYear - startYear + 1 )
. SelectMany ( year = > Enumerable. Range ( 1 , 12 ) . Select ( month = > new DateTime ( year, month, 13 ) ) )
. Where ( date = > date. DayOfWeek == DayOfWeek. Friday)
. Select ( date = > date. ToString ( "M'/'d'/'yyyy" , CultureInfo. InvariantCulture) ) ) ;
}
答案3:
using System;
using System. Collections. Generic;
public static class Kata
{
public static string FridayTheThirteenths ( int Start, int End = int . MinValue)
{
DateTime start = new DateTime ( Start, 1 , 01 ) ;
List< string > dates = new List < string > ( ) ;
while ( start. Year== Start || start. Year<= End)
{
if ( start. DayOfWeek == DayOfWeek. Friday & amp; & amp; start. Day == 13 )
{
dates. Add ( start. ToString ( "M/dd/yyyy" ) ) ;
}
start = start. AddDays ( 1 ) ;
}
return string . Join ( " " , dates. ToArray ( ) ) ;
}
}
答案4:
using System;
using System. Globalization;
public static class Kata
{
public static string FridayTheThirteenths ( int Start, int End = int . MinValue)
{
var res = "" ;
if ( End == int . MinValue)
End = Start;
var endDate = new DateTime ( End+ 1 , 1 , 1 ) ;
for ( var curDate = new DateTime ( Start, 1 , 1 ) ; curDate < endDate; curDate = curDate. AddMonths ( 1 ) )
{
var date = curDate. AddDays ( 12 ) ;
if ( date. DayOfWeek == DayOfWeek. Friday)
res + = String. Format ( CultureInfo. CreateSpecificCulture ( "en-US" ) , "{0:M/d/yyyy }" , date) ;
}
return res. Remove ( res. Length- 1 ) ;
}
}
答案5:
using System;
using System. Linq;
using System. Collections. Generic;
public static class Kata
{
public static string FridayTheThirteenths ( int Start, int End = int . MinValue)
{
End = End == int . MinValue ? Start : End;
var months = new int [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 } ;
var dateList = new List < DateTime > ( ) ;
for ( var i = Start; i <= End; i++ )
{
dateList. AddRange ( months. Select ( thisMonth = > new DateTime ( year: i, month: thisMonth, day: 13 ) ) . Where ( date = > date. DayOfWeek == DayOfWeek. Friday) ) ;
}
return string . Join ( " " , dateList. Select ( date = > date. ToString ( "M/dd/yyyy" ) ) ) ;
}
}
答案6:
using System;
using System. Linq;
public static class Kata
{
public static string FridayTheThirteenths ( int Start, int End = int . MinValue) = >
string . Join ( " " , ( End == int . MinValue ? new int [ ] { Start } :
Enumerable. Range ( Start, End - Start+ 1 ) ) . SelectMany ( y = >
"1 2 3 4 5 6 7 8 9 10 11 12" . Split ( ) . Select ( m = >
new DateTime ( y, int . Parse ( m) , 13 ) . DayOfWeek == DayOfWeek. Friday ?
$"{m}/13/{y}" : "" ) . Where ( x = > x != "" ) ) ) ;
}
答案7:
using System;
public static class Kata
{
public static string FridayTheThirteenths ( int Start, int End = int . MinValue)
{
DateTime currentDate = new DateTime ( Start, 1 , 13 ) ;
DateTime endDate;
if ( End == int . MinValue) {
endDate = new DateTime ( Start, 12 , 13 ) ;
}
else {
endDate = new DateTime ( End, 12 , 13 ) ;
}
string result = "" ;
while ( DateTime. Compare ( currentDate, endDate) <= 0 ) {
if ( currentDate. DayOfWeek == DayOfWeek. Friday) result + = currentDate. Date. ToString ( "M/dd/yyyy" ) + " " ;
currentDate = currentDate. AddMonths ( 1 ) ;
}
return result. Trim ( ) ;
}
}
答案8:
using System;
using System. Linq;
public static class Kata
{
public static string FridayTheThirteenths ( int Start, int End = int . MinValue)
{
if ( End == int . MinValue) End = Start;
var span = ( End - Start + 1 ) * 12 ;
var unluckyDays = Enumerable. Range ( 0 , span) . Select ( i = > new DateTime ( Start, 1 , 13 ) . AddMonths ( i) ) . Where ( d = > d. DayOfWeek == DayOfWeek. Friday) ;
return string . Join ( " " , unluckyDays. Select ( d = > d. ToString ( "M/d/yyyy" ) ) ) ;
}
}
答案9:
using System;
using System. Collections. Generic;
using System. Globalization;
public static class Kata
{
public static string FridayTheThirteenths ( int Start, int End = int . MinValue)
{
List< string > result = new List < string > ( ) ;
if ( End == int . MinValue)
{
for ( int month = 1 ; month <= 12 ; month++ )
{
DateTime testDate = new DateTime ( Start, month, 13 ) ;
if ( testDate. DayOfWeek == DayOfWeek. Friday)
result. Add ( testDate. ToString ( "M/d/yyyy" ) ) ;
}
}
else
{
for ( int year = Start; year <= End; year++ )
{
for ( int month = 1 ; month <= 12 ; month++ )
{
DateTime testDate = new DateTime ( year, month, 13 ) ;
if ( testDate. DayOfWeek == DayOfWeek. Friday)
result. Add ( testDate. ToString ( "M/d/yyyy" ) ) ;
}
}
}
return string . Join ( " " , result) ;
}
}
答案10:
using System;
using System. Collections;
using System. Collections. Generic;
using System. Linq;
public static class Kata
{
public static string FridayTheThirteenths ( int Start, int End = int . MinValue)
{
int yend = ( End == int . MinValue) ? Start : End;
List< DateTime> fridays = new List < DateTime > ( ) ;
DateTime dt = new DateTime ( yend, 1 , 1 ) ;
string s = "" ;
for ( int i= Start; i<= yend; i++ )
{
for ( int j= 1 ; j<= 12 ; j++ )
{
dt = new DateTime ( i, j, 13 ) ;
if ( dt. DayOfWeek. ToString ( ) == "Friday" )
fridays. Add ( dt) ;
}
}
foreach ( var el in fridays)
s + = el. Month + "/" + el. Day + "/" + el. Year + " " ;
return s. Substring ( 0 , s. Length- 1 ) ;
}
}