给定开始日期和一个月中的天数。查找一个月中每天发生的次数
例子:
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.
观察:我们必须做一些关键的观察。第一个是如果一个月有28天,则每天发生4次。第二个是如果有29天,则该月开始的日期将发生5次。第三个是如果该月有30天,则该月开始的那一天,第二天将发生5天。最后一个是如果该月有31天,则该月开始的那一天以及接下来的2天将发生5天,其余各发生4次。
方法:创建一个大小为7且初始值为4的计数数组,因为最小出现次数将是4。 (天数-28) 。查找第一天的索引。计算发生次数为5的天数,然后运行从pos到pos +(天数-28)的循环,将发生次数标记为5。如果pos +(天数28)超过6,则使用%7从一开始就进入索引。
以下是上述方法的C++实现:
CPP
// C++ program to count occurrence of days in a month
#include
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
输出:
Monday 4
Tuesday 5
Wednesday 5
Thursday 5
Friday 4
Saturday 4
Sunday 4