Dado el día de inicio y el número de días de un mes. Encuentre la cantidad de veces que ocurre cada día en un mes
Ejemplos:
Input : Number of days in month = 28 First day = Wednesday Output : Monday = 4 Tuesday = 4 Wednesday = 4 Thursday = 4 Friday = 4 Saturday = 4 Sunday = 4 Explanation: In the month of February, every day occurs 4 times. Input : Number of days in month = 31 First day = Tuesday Output : Monday = 4 Tuesday = 5 Wednesday = 5 Thursday = 5 Friday = 4 Saturday = 4 Sunday = 4 Explanation: The month starts on Tuesday and ends on Thursday.
Observaciones: Tenemos que hacer algunas observaciones clave. El primero será si el mes tiene 28 días, entonces cada día ocurre 4 veces. El segundo será si tiene 29 días, entonces el día en que comienza el mes ocurrirá 5 veces. El tercero será si el mes tiene 30 días, entonces el día en que comienza el mes y el día siguiente ocurrirá 5 días. El último es si el mes tiene 31 días, entonces el día en que comienza el mes y los próximos 2 días ocurrirán 5 días y el resto ocurrirá 4 veces cada uno.
Enfoque: cree una array de conteo con tamaño 7 y que tenga un valor inicial de 4, ya que el número mínimo de ocurrencias será 4. (número de días: 28). Encuentre el índice del primer día. Calcule el número de días cuya ocurrencia será 5. Luego ejecute un ciclo desde pos a pos+(número de días-28) para marcar la ocurrencia como 5. Si pos+(número de días-28) excede 6, entonces use %7 para llegar a los índices desde el principio.
A continuación se muestra la implementación en C++ del enfoque anterior:
CPP
// C++ program to count occurrence of days in a month #include <bits/stdc++.h> using namespace std; // function to find occurrences void occurrenceDays(int n, string firstday) { // stores days in a week string days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; // Initialize all counts as 4. int count[7]; for (int i = 0; i < 7; i++) count[i] = 4; // find index of the first day int pos; for (int i = 0; i < 7; i++) { if (firstday == days[i]) { pos = i; break; } } // number of days whose occurrence will be 5 int inc = n - 28; // mark the occurrence to be 5 of n-28 days for (int i = pos; i < pos + inc; i++) { if (i > 6) count[i % 7] = 5; else count[i] = 5; } // print the days for (int i = 0; i < 7; i++) { cout << days[i] << " " << count[i] << endl; } } // driver program to test the above function int main() { int n = 31; string firstday = "Tuesday"; occurrenceDays(n, firstday); return 0; }
Java
// Java program to count // occurrence of days in a month import java.util.*; import java.lang.*; public class GfG{ // function to find occurrences public static void occurrenceDays(int n, String firstday) { // stores days in a week String[] days = new String[]{ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; // Initialize all counts as 4. int[] count = new int[7]; for (int i = 0; i < 7; i++) count[i] = 4; // find index of the first day int pos = 0; for (int i = 0; i < 7; i++) { if (firstday == days[i]) { pos = i; break; } } // number of days whose occurrence // will be 5 int inc = n - 28; // mark the occurrence to be 5 of n-28 days for (int i = pos; i < pos + inc; i++) { if (i > 6) count[i % 7] = 5; else count[i] = 5; } // print the days for (int i = 0; i < 7; i++) { System.out.println(days[i] + " " + count[i]); } } // Driver function public static void main(String argc[]){ int n = 31; String firstday = "Tuesday"; occurrenceDays(n, firstday); } } // This code is contributed by Sagar Shukla
Python3
# Python program to count # occurrence of days in a month import math # function to find occurrences def occurrenceDays( n, firstday): # stores days in a week days = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ] # Initialize all counts as 4. count= [4 for i in range(0,7)] # find index of the first day pos=-1 for i in range(0,7): if (firstday == days[i]): pos = i break # number of days whose occurrence will be 5 inc = n - 28 # mark the occurrence to be 5 of n-28 days for i in range( pos, pos + inc): if (i > 6): count[i % 7] = 5 else: count[i] = 5 # print the days for i in range(0,7): print (days[i] , " " , count[i]) # driver program to test # the above function n = 31 firstday = "Tuesday" occurrenceDays(n, firstday) # This code is contributed by Gitanjali.
C#
// C# program to count occurrence of // days in a month using System; public class GfG { // function to find occurrences public static void occurrenceDays(int n, string firstday) { // stores days in a week String[] days = new String[]{ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; // Initialize all counts as 4. int[] count = new int[7]; for (int i = 0; i < 7; i++) count[i] = 4; // find index of the first day int pos = 0; for (int i = 0; i < 7; i++) { if (firstday == days[i]) { pos = i; break; } } // number of days whose occurrence // will be 5 int inc = n - 28; // mark the occurrence to be 5 of // n-28 days for (int i = pos; i < pos + inc; i++) { if (i > 6) count[i % 7] = 5; else count[i] = 5; } // print the days for (int i = 0; i < 7; i++) { Console.WriteLine(days[i] + " " + count[i]); } } // Driver function public static void Main() { int n = 31; string firstday = "Tuesday"; occurrenceDays(n, firstday); } } // This code is contributed by vt_m.
Javascript
<script> // JavaScript program to count // occurrence of days in a month // function to find occurrences function occurrenceDays(n, firstday) { // stores days in a week let days = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ]; // Initialize all counts as 4. let count = []; for (let i = 0; i < 7; i++) count[i] = 4; // find index of the first day let pos = 0; for (let i = 0; i < 7; i++) { if (firstday == days[i]) { pos = i; break; } } // number of days whose occurrence // will be 5 let inc = n - 28; // mark the occurrence to be 5 of n-28 days for (let i = pos; i < pos + inc; i++) { if (i > 6) count[i % 7] = 5; else count[i] = 5; } // print the days for (let i = 0; i < 7; i++) { document.write(days[i] + " " + count[i] + "<br/>"); } } // Driver code let n = 31; let firstday = "Tuesday"; occurrenceDays(n, firstday); </script>
Producción:
Monday 4 Tuesday 5 Wednesday 5 Thursday 5 Friday 4 Saturday 4 Sunday 4