1. 倒霉的于龙最近总是和日期过不去,因为他叕一次面对一大堆日期数据。这一次的原因是那些日期书写的极其不规范,仔细辨别也根本认不清是哪一天,因为很多日期可以理解为多种情况。
2. 以下每个日期中的三个数,年月日的位置是不确定的,所以导致有多种理解。例如:
3. 2/3/1 可理解为0002-03-01或0003-02-01或0001-02-03等6种情况;
4. 13+12+45 只可理解为0045-12-13一种情况;
5. 12=3=13 可理解为0012-03-13或0002-12-13或0013-12-03或0013-03-12四种情况;
6. 29,2,2019 怎么理解都不是合法日期;
7. 35.36.37 怎么理解都不是合法日期;
8. 11*31*30 可理解为0031-11-30一种情况;
9. 11*31*11 可理解为0011-11-30一种情况;
10. 5*5*5 可理解为0005-05-05一种情况;
11. 5*6*6 可理解为0005-06-06、0006-06-05、0006-05-06三种情况;
12. 13-13-5 可理解为0013-05-13一种情况;
13. 5-5-13 可理解为0013-05-05、0005-05-13两种情况;
14.
15. 现在于龙要编程判断一下那些日期到底是否可以理解成合法日期,你们说他能编出来吗。
输入格式:
1. 一行,以X-Y-Z形式表示的一个日期,其中X、Y、Z为不超过4位的正整数,“-”为一个分隔字符,可能为任何符号。
2. 注意:这次测试数据不保证X、Y、Z各不相同。
输出格式:
1. 若该日期可以理解为合法存在的日期,首先输出它可以被理解成的合法日期个数,然后按从后往前的顺序依次输出这些合法日期;
2. 若该日期无法理解成合法日期,输出:Invalid Date!。
// We can start by splitting the input string into three integers using the separator character
// Then we can try to generate all possible date formats using the given rules and check if any of them is a valid date
// If a valid date is found, we can store it in an array and count the number of valid dates
// Finally, we can output the number of valid dates and the dates themselves in reverse order, or "Invalid Date!" if no valid dates were found
// We can start by splitting the input string into three integers using the separator character
// Then we can try to generate all possible date formats using the given rules and check if any of them is a valid date
// If a valid date is found, we can store it in an array and count the number of valid dates
// Finally, we can output the number of valid dates and the dates themselves in reverse order, or "Invalid Date!" if no valid dates were found
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int is_leap_year(int year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
int is_valid_date(int year, int month, int day) {
if (year < 1 || year > 9999 || month < 1 || month > 12 || day < 1) {
return 0;
}
int days_in_month[13] = {0,31, 28 + is_leap_year(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
return day <= days_in_month[month];
}
int main() {
char date_str[20];
printf("Enter a date in the format X-Y-Z: ");
scanf("%s", date_str);
int x, y, z;
char* ptr;
x = strtol(date_str, &ptr, 10);
y = strtol(ptr + 1, &ptr, 10);
z = strtol(ptr + 1, &ptr, 10);
if(x<y)
{
x=x^y;
y=x^y;
x=x^y;
}
if(x<z)
{
x=x^z;
z=x^z;
x=x^z;
}
if(y<z)
{
y=z^y;
z=z^y;
y=z^y;
}
int valid_dates[6][3];
int num_valid_dates = 0;
if (is_valid_date(x, y, z)) {
valid_dates[num_valid_dates][0] = x;
valid_dates[num_valid_dates][1] = y;
valid_dates[num_valid_dates][2] = z;
num_valid_dates++;
}
if (is_valid_date(x, z, y)) {
valid_dates[num_valid_dates][0] = x;
valid_dates[num_valid_dates][1] = z;
valid_dates[num_valid_dates][2] = y;
num_valid_dates++;
}
if (is_valid_date(y, x, z)) {
valid_dates[num_valid_dates][0] = y;
valid_dates[num_valid_dates][1] = x;
valid_dates[num_valid_dates][2] = z;
num_valid_dates++;
}
if (is_valid_date(y, z, x)) {
valid_dates[num_valid_dates][0] = y;
valid_dates[num_valid_dates][1] = z;
valid_dates[num_valid_dates][2] = x;
num_valid_dates++;
}
if (is_valid_date(z, x, y)) {
valid_dates[num_valid_dates][0] = z;
valid_dates[num_valid_dates][1] = x;
valid_dates[num_valid_dates][2] = y;
num_valid_dates++;
}
if (is_valid_date(z, y, x)) {
valid_dates[num_valid_dates][0] = z;
valid_dates[num_valid_dates][1] = y;
valid_dates[num_valid_dates][2] = x;
num_valid_dates++;
}
if (num_valid_dates == 0) {
printf("Invalid Date!\n");
} else {
printf("%d\n", num_valid_dates);
for (int i = num_valid_dates - 1; i >= 0; i--) {
printf("%04d-%02d-%02d\n", valid_dates[i][0], valid_dates[i][1], valid_dates[i][2]);
}
}
return 0;
}