给您年份Y,找到下一个与Y相同的日历年。
例子 :
Input : 2017
Output : 2023
Input : 2018
Output : 2029
如果满足以下两个条件,则年份x与给定的年份y相同。
- x从y的同一天开始。
- 如果y是leap年,则x也是。如果y不是leap年,则x也不是。
想法是逐年检查所有年份(从明年开始)。我们跟踪前进的天数。如果总移动天数是7,则当前年份从同一天开始。我们还检查当年的跨度是否与y相同。如果两个条件都满足,我们将返回当前年份。
C++
// C++ program to find next identical year
#include
using namespace std;
// Function for finding extra days of year
// more than complete weeks
int extraDays(int y)
{
// If current year is a leap year, then
// it number of weekdays move ahead by
// 2 in terms of weekdays.
if (y%400==0 || y%100!=0 && y%4==0)
return 2;
// Else number of weekdays move ahead
// by 1.
return 1;
}
// Returns next identical year.
int nextYear(int y)
{
// Find number of days moved ahead by y
int days = extraDays(y);
// Start from next year
int x = y + 1;
// Count total number of weekdays
// moved ahead so far.
for (int sum=0; ; x++)
{
sum = (sum + extraDays(x)) % 7;
// If sum is divisible by 7 and leap-ness
// of x is same as y, return x.
if ( sum==0 && (extraDays(x) == days))
return x;
}
return x;
}
// driver program
int main()
{
int y = 2018;
cout << nextYear(y);
return 0;
}
Java
// Java program to find next identical year
class GFG {
// Function for finding extra days of year
// more than complete weeks
static int extraDays(int y)
{
// If current year is a leap year, then
// it number of weekdays move ahead by
// 2 in terms of weekdays.
if (y % 400 == 0 || y % 100 != 0 && y % 4 == 0)
return 2;
// Else number of weekdays move ahead
// by 1.
return 1;
}
// Returns next identical year.
static int nextYear(int y)
{
// Find number of days moved ahead by y
int days = extraDays(y);
// Start from next year
int x = y + 1;
// Count total number of weekdays
// moved ahead so far.
for (int sum = 0; ; x++)
{
sum = (sum + extraDays(x)) % 7;
// If sum is divisible by 7 and leap-ness
// of x is same as y, return x.
if ( sum == 0 && (extraDays(x) == days))
return x;
}
}
// Driver code
public static void main(String[] args)
{
int y = 2018;
System.out.println(nextYear(y));
}
}
/* This code contributed by PrinciRaj1992 */
C#
// C# program to find next identical year
using System;
class GFG
{
// Function for finding extra days of year
// more than complete weeks
static int extraDays(int y)
{
// If current year is a leap year, then
// it number of weekdays move ahead by
// 2 in terms of weekdays.
if (y % 400 == 0 || y % 100 != 0 && y % 4 == 0)
return 2;
// Else number of weekdays move ahead
// by 1.
return 1;
}
// Returns next identical year.
static int nextYear(int y)
{
// Find number of days moved ahead by y
int days = extraDays(y);
// Start from next year
int x = y + 1;
// Count total number of weekdays
// moved ahead so far.
for (int sum = 0; ; x++)
{
sum = (sum + extraDays(x)) % 7;
// If sum is divisible by 7 and leap-ness
// of x is same as y, return x.
if ( sum == 0 && (extraDays(x) == days))
return x;
}
}
// Driver code
public static void Main(String[] args)
{
int y = 2018;
Console.WriteLine(nextYear(y));
}
}
// This code has been contributed by 29AjayKumar
PHP
Javascript
输出 :
2029