Encuentra el próximo año calendario idéntico

Se le da un año Y, encuentre el próximo año calendario idéntico a Y.
Ejemplos: 
 

Input : 2017
Output : 2023

Input : 2018
Output : 2029

Un año x es idéntico a un año anterior dado y si se cumplen las siguientes dos condiciones. 
 

  1. x comienza con el mismo día que y.
  2. Si y es un año bisiesto, entonces x también lo es. Si y no es un año bisiesto, entonces x tampoco lo es.

La idea es verificar todos los años uno por uno (a partir del próximo año). Realizamos un seguimiento del número de días adelantados. Si el total de días movidos es 7, entonces el año actual comienza con el mismo día. También verificamos si el salto del año actual es el mismo que y. Si se cumplen ambas condiciones, devolvemos el año actual.
 

C++

// C++ program to find next identical year
#include<iostream>
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 */

Python3

# Python3 program to find next identical year
 
# Function for finding extra days of year
# more than complete weeks
def extraDays(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 or y % 100 != 0 and y % 4 == 0) :
        return 2
 
    # Else number of weekdays move ahead
    # by 1.
    return 1
 
# Returns next identical year.
def nextYear(y) :
 
    # Find number of days moved ahead by y
    days = extraDays(y)
 
    # Start from next year
    x = y + 1
 
    # Count total number of weekdays
    # moved ahead so far.
    Sum = 0
    while(True) :
 
        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 and (extraDays(x) == days)) :
            return x
             
        x += 1
 
    return x
 
y = 2018
print(nextYear(y))
 
# This code is contributed by mukesh07.

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

<?php
// PHP program to find
// next identical year
 
// Function for finding extra days
// of year more than complete weeks
 
function extraDays($y)
{
    // If current year is a leap year,
    // then 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.
function nextYear($y)
{
    // Find number of days
    // moved ahead by y
    $days = extraDays($y);
 
    // Start from next year
    $x = $y + 1;
 
    // Count total number of weekdays
    // moved ahead so far.
    for ($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 Code
$y = 2018;
echo nextYear($y);
 
// This code is contributed by aj_36
?>

Javascript

<script>
 
// JavaScript program for the above approach
 
// Function for finding extra days of year
// more than complete weeks
function extraDays(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.
function nextYear(y)
{
    // Find number of days moved ahead by y
    let days = extraDays(y);
 
    // Start from next year
    let x = y + 1;
 
    // Count total number of weekdays
    // moved ahead so far.
    for (let 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
    let y = 2018;
    document.write(nextYear(y));
         
        // This code is contributed by susmitakundugoaldanga.
</script>

Producción : 
 

2029

Este artículo es una contribución de Shivam Pradhan (anuj_charm) . Si le gusta GeeksforGeeks y le gustaría contribuir, también puede escribir un artículo usando contribuya.geeksforgeeks.org o envíe su artículo por correo a contribuya@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *