给定一个正整数D和一个字符串M代表闰年的日期和月份,任务是找到下半年之后的日期。
例子:
Input: D = 15, M = “January”
Output: 16 July
Explanation: The date from the 15th of January to the next half year is 16th of July.
Input: D = 10, M = “October”
Output: 10 April
方法:由于闰年包含366 天,因此可以通过将当前日期增加183 天后查找数据来解决给定的问题。请按照以下步骤解决问题:
- 将每个月的天数存储在该数组中,例如days[] 。
- 初始化一个变量,比如curMonth为M ,以存储当月的索引。
- 初始化一个变量,比如curDate为D ,以存储当前日期。
- 初始化一个变量,比如count为183 ,表示要增加的天数。
- 迭代直到count 的值为正,然后执行以下步骤:
- 如果计数值为正,CURDATE是在curMonth大部分天数则减1计数的值和递增1 CURDATE的价值。
- 如果count 的值为0,则跳出循环。
- 按(curMonth + 1)%12更新curMonth的值。
- 完成上述步骤后,打印出curDate和curMonth对应的日期作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the date
// after the next half-year
void getDate(int d, string m) {
// Stores the number of days in the
// months of a leap year
int days[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// List of months
string month[] = {"January", "February",
"March", "April",
"May", "June",
"July", "August",
"September", "October",
"November", "December"};
// Days in half of a year
int cnt = 183;
// Index of current month
int cur_month;
for(int i = 0; i < 12; i++)
if(m == month[i])
cur_month = i;
// Starting day
int cur_date = d;
while(1) {
while(cnt > 0 && cur_date <= days[cur_month]) {
// Decrement the value of
// cnt by 1
cnt -= 1;
// Increment cur_date
cur_date += 1;
}
// If cnt is equal to 0, then
// break out of the loop
if(cnt == 0)
break;
// Update cur_month
cur_month = (cur_month + 1) % 12;
// Update cur_date
cur_date = 1;
}
// Print the resultant date
cout << cur_date << " " << month[cur_month] << endl;
}
// Driver Code
int main() {
int D = 15;
string M = "January";
// Function Call
getDate(D, M);
return 0;
}
// This code is contributed by Dharanendra L V.
Java
// Java program for the above approach
class GFG{
// Function to find the date
// after the next half-year
public static void getDate(int d, String m)
{
// Stores the number of days in the
// months of a leap year
int[] days = { 31, 29, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
// List of months
String[] month = { "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December" };
// Days in half of a year
int cnt = 183;
// Index of current month
int cur_month = 0;
for(int i = 0; i < 12; i++)
if (m == month[i])
cur_month = i;
// Starting day
int cur_date = d;
while (true)
{
while (cnt > 0 && cur_date <= days[cur_month])
{
// Decrement the value of
// cnt by 1
cnt -= 1;
// Increment cur_date
cur_date += 1;
}
// If cnt is equal to 0, then
// break out of the loop
if (cnt == 0)
break;
// Update cur_month
cur_month = (cur_month + 1) % 12;
// Update cur_date
cur_date = 1;
}
// Print the resultant date
System.out.println(cur_date + " " +
month[cur_month]);
}
// Driver Code
public static void main(String args[])
{
int D = 15;
String M = "January";
// Function Call
getDate(D, M);
}
}
// This code is contributed by SoumikMondal
Python3
# Python program for the above approach
# Function to find the date
# after the next half-year
def getDate(d, m):
# Stores the number of days in the
# months of a leap year
days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# List of months
month = ['January', 'February',
'March', 'April',
'May', 'June',
'July', 'August',
'September', 'October',
'November', 'December']
# Days in half of a year
cnt = 183
# Index of current month
cur_month = month.index(m)
# Starting day
cur_date = d
while(1):
while(cnt > 0 and cur_date <= days[cur_month]):
# Decrement the value of
# cnt by 1
cnt -= 1
# Increment cur_date
cur_date += 1
# If cnt is equal to 0, then
# break out of the loop
if(cnt == 0):
break
# Update cur_month
cur_month = (cur_month + 1) % 12
# Update cur_date
cur_date = 1
# Print the resultant date
print(cur_date, month[cur_month])
# Driver Code
D = 15
M = "January"
# Function Call
getDate(D, M)
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the date
// after the next half-year
static void getDate(int d, string m)
{
// Stores the number of days in the
// months of a leap year
int[] days = { 31, 29, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
// List of months
string[] month = { "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December" };
// Days in half of a year
int cnt = 183;
// Index of current month
int cur_month = 0;
for(int i = 0; i < 12; i++)
if (m == month[i])
cur_month = i;
// Starting day
int cur_date = d;
while (true)
{
while (cnt > 0 && cur_date <= days[cur_month])
{
// Decrement the value of
// cnt by 1
cnt -= 1;
// Increment cur_date
cur_date += 1;
}
// If cnt is equal to 0, then
// break out of the loop
if (cnt == 0)
break;
// Update cur_month
cur_month = (cur_month + 1) % 12;
// Update cur_date
cur_date = 1;
}
// Print the resultant date
Console.WriteLine(cur_date + " " +
month[cur_month]);
}
// Driver Code
public static void Main()
{
int D = 15;
string M = "January";
// Function Call
getDate(D, M);
}
}
// This code is contributed by ukasp
Javascript
输出:
16 July
时间复杂度: O(1)
辅助空间: O(1)