给定两个年Y1和Y2,其中10≤3≤Y1 Y2≤10 6,任务是找到并打印所有这一切都是在给定年间回文的日期。
例子:
Input: Y1 = 2001, Y2 = 2005
Output:
10022001
20022002
Input: Y1 = 5000, Y2 = 5010
Output:
10055001
20055002
30055003
01055010
方法:由于前四位数字(即日期和月份)必须与后四位数字(年份)相反,才能使日期成为回文。因此,每年最多会有一个回文日期。为了找到日期,请每年将年份数字倒转并检查形成的日期是否有效。如果有效,则打印它。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
const int MAX_VALID_YR = 9999;
const int MIN_VALID_YR = 1800;
// Returns true if
// given year is valid
bool isLeap(int year)
{
// Return true if year
// is a multiple pf 4 and
// not multiple of 100.
// OR year is multiple of 400.
return (((year % 4 == 0)
&& (year % 100 != 0))
|| (year % 400 == 0));
}
// Returns true if given
// year is valid or not.
bool isValidDate(int d, int m, int y)
{
// If year, month and day
// are not in given range
if (y > MAX_VALID_YR || y < MIN_VALID_YR)
return false;
if (m < 1 || m > 12)
return false;
if (d < 1 || d > 31)
return false;
// Handle February month
// with leap year
if (m == 2) {
if (isLeap(y))
return (d <= 29);
else
return (d <= 28);
}
// Months of April, June,
// Sept and Nov must have
// number of days less than
// or equal to 30.
if (m == 4 || m == 6 || m == 9 || m == 11)
return (d <= 30);
return true;
}
// Function to print the palindrome
// dates between the given years
void printPalindromeDates(int y1, int y2)
{
// For every year
for (int year = y1; year <= y2; year++) {
// Current year as a string
string str = to_string(year);
// To store the reverse of year
string rev = str;
reverse(rev.begin(), rev.end());
// Get the day and the month
int day = stoi(rev.substr(0, 2));
int month = stoi(rev.substr(2, 2));
// If the current palindrome date is valid
if (isValidDate(day, month, year)) {
cout << rev << str << endl;
}
}
}
// Driver code
int main()
{
int y1 = 2001, y2 = 2005;
printPalindromeDates(y1, y2);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static int MAX_VALID_YR = 9999;
static int MIN_VALID_YR = 1800;
// Returns true if
// given year is valid
static boolean isLeap(int year)
{
// Return true if year
// is a multiple pf 4 and
// not multiple of 100.
// OR year is multiple of 400.
return (((year % 4 == 0)
&& (year % 100 != 0))
|| (year % 400 == 0));
}
// Returns true if given
// year is valid or not.
static boolean isValidDate(int d, int m, int y)
{
// If year, month and day
// are not in given range
if (y > MAX_VALID_YR || y < MIN_VALID_YR)
return false;
if (m < 1 || m > 12)
return false;
if (d < 1 || d > 31)
return false;
// Handle February month
// with leap year
if (m == 2)
{
if (isLeap(y))
return (d <= 29);
else
return (d <= 28);
}
// Months of April, June,
// Sept and Nov must have
// number of days less than
// or equal to 30.
if (m == 4 || m == 6 || m == 9 || m == 11)
return (d <= 30);
return true;
}
// Function to print the palindrome
// dates between the given years
static void printPalindromeDates(int y1, int y2)
{
// For every year
for (int year = y1; year <= y2; year++)
{
// Current year as a String
String str = String.valueOf(year);
// To store the reverse of year
String rev = str;
rev = reverse(rev);
// Get the day and the month
int day = Integer.parseInt(rev.substring(0, 2));
int month = Integer.parseInt(rev.substring(2, 2 + 2));
// If the current palindrome date is valid
if (isValidDate(day, month, year))
{
System.out.print(rev + str +"\n");
}
}
}
static String reverse(String input)
{
char[] a = input.toCharArray();
int l, r = a.length - 1;
for (l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
// Driver code
public static void main(String[] args)
{
int y1 = 2001, y2 = 2005;
printPalindromeDates(y1, y2);
}
}
// This code is contributed by PrinciRaj1992
Python 3
# Python 3 implementation of the approach
MAX_VALID_YR = 9999
MIN_VALID_YR = 1800
# Returns true if given year is valid
def isLeap(year):
# Return true if year
# is a multiple pf 4 and
# not multiple of 100.
# OR year is multiple of 400.
return (((year % 4 == 0) and
(year % 100 != 0)) or
(year % 400 == 0))
# Returns true if given
# year is valid or not.
def isValidDate(d, m, y):
# If year, month and day
# are not in given range
if (y > MAX_VALID_YR or y < MIN_VALID_YR):
return False
if (m < 1 or m > 12):
return False
if (d < 1 or d > 31):
return False
# Handle February month
# with leap year
if (m == 2):
if (isLeap(y)):
return (d <= 29)
else:
return (d <= 28)
# Months of April, June,
# Sept and Nov must have
# number of days less than
# or equal to 30.
if (m == 4 or m == 6 or
m == 9 or m == 11):
return (d <= 30)
return True
# Function to print the palindrome
# dates between the given years
def printPalindromeDates(y1, y2):
# For every year
for year in range(y1, y2 + 1, 1):
# Current year as a string
str1 = str(year)
# To store the reverse of year
rev = str1
rev = rev[::-1]
# Get the day and the month
day = int(rev[0 : 2])
month = int(rev[2 : 4])
# If the current palindrome date is valid
rev += str1
if (isValidDate(day, month, year)):
print(rev)
# Driver code
if __name__ == '__main__':
y1 = 2001
y2 = 2005
printPalindromeDates(y1, y2)
# This code is contributed by Surendra_Gangwar
C#
// C# implementation of the approach
using System;
class GFG
{
static int MAX_VALID_YR = 9999;
static int MIN_VALID_YR = 1800;
// Returns true if
// given year is valid
static bool isLeap(int year)
{
// Return true if year
// is a multiple pf 4 and
// not multiple of 100.
// OR year is multiple of 400.
return (((year % 4 == 0) &&
(year % 100 != 0)) ||
(year % 400 == 0));
}
// Returns true if given
// year is valid or not.
static bool isValidDate(int d, int m, int y)
{
// If year, month and day
// are not in given range
if (y > MAX_VALID_YR || y < MIN_VALID_YR)
return false;
if (m < 1 || m > 12)
return false;
if (d < 1 || d > 31)
return false;
// Handle February month
// with leap year
if (m == 2)
{
if (isLeap(y))
return (d <= 29);
else
return (d <= 28);
}
// Months of April, June,
// Sept and Nov must have
// number of days less than
// or equal to 30.
if (m == 4 || m == 6 || m == 9 || m == 11)
return (d <= 30);
return true;
}
// Function to print the palindrome
// dates between the given years
static void printPalindromeDates(int y1, int y2)
{
// For every year
for (int year = y1; year <= y2; year++)
{
// Current year as a String
String str = String.Join("", year);
// To store the reverse of year
String rev = str;
rev = reverse(rev);
// Get the day and the month
int day = Int32.Parse(rev.Substring(0, 2));
int month = Int32.Parse(rev.Substring(2, 2));
// If the current palindrome date is valid
if (isValidDate(day, month, year))
{
Console.Write(rev + str +"\n");
}
}
}
static String reverse(String input)
{
char[] a = input.ToCharArray();
int l, r = a.Length - 1;
for (l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join("", a);
}
// Driver code
public static void Main(String[] args)
{
int y1 = 2001, y2 = 2005;
printPalindromeDates(y1, y2);
}
}
// This code is contributed by 29AjayKumar
输出:
10022001
20022002