Advertisement
cogier

Five weekends

Mar 27th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.71 KB | None | 0 0
  1. 'https://rosettacode.org/wiki/Five_weekends
  2. Imports System
  3. Module Program
  4.     Sub Main()
  5.  
  6.         Dim aMonth As Short() = {1, 3, 5, 7, 8, 10, 12}              'All 31 day months
  7.         Dim aMMStore As New List(Of String)                          'To store results
  8.         Dim siYear, siMonth, siCount As Short                        'Various variables
  9.         Dim dDay As Date                                             'To store the day to check
  10.         Dim sTemp As String                                          'Temp string
  11.  
  12.         For siYear = 1900 To 2100                                    'Loop through each year
  13.             For siMonth = 0 To 6                                     'Loop through each 31 day month
  14.                 dDay = New Date(siYear, aMonth(siMonth), 1)          'Get the date of the 1st of the month
  15.                 If Weekday(dDay) = 6 Then
  16.                     aMMStore.Add(Format(dDay, "MMMM yyyy"))          'If the 1st is a Friday then store the result
  17.                 End If
  18.             Next
  19.         Next
  20.  
  21.         For Each sTemp In aMMStore 'For each item in the stored array..
  22.             siCount += 1 'Increase siCount
  23.             If siCount < 6 Then Console.WriteLine(aMMStore(siCount))                        'If 1 of the 1st 5 dates then print it
  24.             If siCount = 6 Then Console.WriteLine(New String("-", 14))                      'Print a separator
  25.             If siCount > aMMStore.Count - 5 Then Console.WriteLine(aMMStore(siCount - 1))   'If 1 of the last 5 dates then print it
  26.         Next
  27.  
  28.         Console.WriteLine("Total months = " & Str(siCount))          'Print the number of months found
  29.  
  30.         siCount = 0                                                  'Reset siCount
  31.         sTemp = String.Join(",", aMMStore)                           'Put all the stored dates in one string joined by commas
  32.         aMMStore.Clear() 'Clear the store for reuse
  33.  
  34.         For siYear = 1900 To 2100                                    'Loop through each year
  35.             If InStr(sTemp, Str(siYear)) = 0 Then                    'If the year is not in the stored string then..
  36.                 siCount += 1                                         'Increase siCount (Amount of years that don't have 5 weekend months)
  37.                 aMMStore.Add(Str(siYear))                            'Add to the store
  38.             End If
  39.         Next
  40.  
  41.         Console.WriteLine()
  42.         Console.WriteLine("There are " & Str(siCount) &
  43.                           " years that do not have at least one five-weekend month")    'Print the amount of years with no 5 weekend months
  44.         Console.WriteLine(String.Join(",", aMMStore))                                   'Print t
  45.  
  46.     End Sub
  47. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement