鱼C论坛

 找回密码
 立即注册
查看: 1268|回复: 6

[已解决]急急急

[复制链接]
发表于 2023-11-12 21:23:33 | 显示全部楼层 |阅读模式
20鱼币
任务描述
倒霉的于龙最近总是和日期过不去,因为他叕一次面对一大堆日期数据。这一次的原因是那些日期书写的极其不规范,仔细辨别也根本认不清是哪一天,因为很多日期可以理解为多种情况。
以下每个日期中的三个数,年月日的位置是不确定的,所以导致有多种理解。例如:
2/3/1     可理解为0002-03-01或0003-02-01或0001-02-03等6种情况;
13+12+45  只可理解为0045-12-13一种情况;
12=3=13   可理解为0012-03-13或0002-12-13或0013-12-03或0013-03-12四种情况;
29,2,2019 怎么理解都不是合法日期;
35.36.37  怎么理解都不是合法日期;
11*31*30  可理解为0031-11-30一种情况;
11*31*11  可理解为0011-11-30一种情况;
5*5*5     可理解为0005-05-05一种情况;
5*6*6     可理解为0005-06-06、0006-06-05、0006-05-06三种情况;
13-13-5   可理解为0013-05-13一种情况;
5-5-13    可理解为0013-05-05、0005-05-13两种情况;
现在于龙要编程判断一下那些日期到底是否可以理解成合法日期,你们说他能编出来吗。
输入格式:
一行,以X-Y-Z形式表示的一个日期,其中X、Y、Z为不超过4位的正整数,“-”为一个分隔字符,可能为任何符号。
注意:这次测试数据不保证X、Y、Z各不相同。
输出格式:
若该日期可以理解为合法存在的日期,首先输出它可以被理解成的合法日期个数,然后按从后往前的顺序依次输出这些合法日期;
若该日期无法理解成合法日期,输出:Invalid Date!。
输入样例:
2/3/1
输出样例:
6
0003-02-01
0003-01-02
0002-03-01
0002-01-03
0001-03-02
0001-02-03
输入样例:
13+12+45
输出样例:
1
0045-12-13
输入样例:
29,2,2019
输出样例:
Invalid Date!
输入样例:
35-36-37
输出样例:
Invalid Date!
输入样例:
11-30-31
输出样例:
1
0031-11-30
输入样例:
5-5-13
输出样例:
2
0013-05-05
0005-05-13
输入样例:
5-5-6
输出样例:
3
0006-05-05
0005-06-05
0005-05-06
输入样例:
4-2-28
输出样例:
4
0028-04-02
0028-02-04
0004-02-28
0002-04-28
输入样例:
5*5*5
输出样例:
1
0005-05-05

用C语言,要输出有几种,然后按从大到小排序
最佳答案
2023-11-12 21:23:34
  1. sh-5.2$ cat main.c
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <string.h>

  5. struct tm_t {size_t year, month, day;};

  6. bool check(const struct tm_t *tm) {
  7.     if(tm->year == 0) return false;
  8.     if(tm->month == 0) return false;
  9.     if(tm->day == 0) return false;
  10.     if(tm->month > 12) return false;
  11.     size_t days[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  12.     if(tm->day > days[tm->month]) return false;
  13.     if(tm->month == 2 && tm->day == 29) return false;
  14.     return true;
  15. }

  16. //bool operator==(const struct tm_t *a, const struct tm_t *b) {
  17. bool operator_equ(const struct tm_t *a, const struct tm_t *b) {
  18.     if(a->year != b->year) return false;
  19.     if(a->month != b->month) return false;
  20.     if(a->day != b->day) return false;
  21.     return true;
  22. }

  23. size_t find(const struct tm_t *dest, size_t size, const struct tm_t *value) {
  24.     for(size_t i = 0; i < size; ++i) {
  25.         //if(dest[i] == *value) return i;
  26.         if(operator_equ(&dest[i], value)) return i;
  27.     }
  28.     return -1;
  29. }

  30. size_t unique(struct tm_t *dest, const struct tm_t *src, size_t size) {
  31.     struct tm_t tmp[size]; memcpy(tmp, src, sizeof(*src) * size);
  32.     size_t new_size = 0;
  33.     for(size_t i = 0; i < size; ++i) {
  34.         if(find(dest, new_size, &tmp[i]) == -1) dest[new_size++] = tmp[i];
  35.     }
  36.     return new_size;
  37. }

  38. int main(void) {
  39.     size_t a, b, c;
  40.     scanf("%zu%*[+-*/=,.]%zu%*[+-*/=,.]%zu", &a, &b, &c);
  41.     const struct tm_t tms[6] = {{a, b, c}, {a, c, b}, {b, a, c}, {b, c, a}, {c, a, b}, {c, b, a}};
  42.     struct tm_t result[6]; size_t size = 0;
  43.     for(size_t i = 0; i < 6; ++i) {
  44.         if(check(&tms[i])) result[size++] = tms[i];
  45.     }
  46.     if(!size) {
  47.         printf("Invalid Date!\n");
  48.         return 0;
  49.     }
  50.     size = unique(result, result, size);
  51.     printf("%zu\n", size);
  52.     for(size_t i = 0; i < size; ++i) {
  53.         printf("%.04zu-%.02zu-%.02zu\n", result[i].year, result[i].month, result[i].day);
  54.     }
  55.     return 0;
  56. }
  57. sh-5.2$ ./main
  58. 2/3/1
  59. 6
  60. 0002-03-01
  61. 0002-01-03
  62. 0003-02-01
  63. 0003-01-02
  64. 0001-02-03
  65. 0001-03-02
  66. sh-5.2$ ./main
  67. 13+12+45
  68. 1
  69. 0045-12-13
  70. sh-5.2$ ./main
  71. 29,2,2019
  72. Invalid Date!
  73. sh-5.2$ ./main
  74. 35-36-37
  75. Invalid Date!
  76. sh-5.2$ ./main
  77. 11-30-31
  78. 1
  79. 0031-11-30
  80. sh-5.2$ ./main
  81. 5-5-13
  82. 2
  83. 0005-05-13
  84. 0013-05-05
  85. sh-5.2$ ./main
  86. 5-5-6
  87. 3
  88. 0005-05-06
  89. 0005-06-05
  90. 0006-05-05
  91. sh-5.2$ ./main
  92. 4-2-28
  93. 4
  94. 0004-02-28
  95. 0002-04-28
  96. 0028-04-02
  97. 0028-02-04
  98. sh-5.2$ ./main
  99. 5*5*5
  100. 1
  101. 0005-05-05
  102. sh-5.2$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-12 21:23:34 | 显示全部楼层    本楼为最佳答案   
  1. sh-5.2$ cat main.c
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <string.h>

  5. struct tm_t {size_t year, month, day;};

  6. bool check(const struct tm_t *tm) {
  7.     if(tm->year == 0) return false;
  8.     if(tm->month == 0) return false;
  9.     if(tm->day == 0) return false;
  10.     if(tm->month > 12) return false;
  11.     size_t days[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  12.     if(tm->day > days[tm->month]) return false;
  13.     if(tm->month == 2 && tm->day == 29) return false;
  14.     return true;
  15. }

  16. //bool operator==(const struct tm_t *a, const struct tm_t *b) {
  17. bool operator_equ(const struct tm_t *a, const struct tm_t *b) {
  18.     if(a->year != b->year) return false;
  19.     if(a->month != b->month) return false;
  20.     if(a->day != b->day) return false;
  21.     return true;
  22. }

  23. size_t find(const struct tm_t *dest, size_t size, const struct tm_t *value) {
  24.     for(size_t i = 0; i < size; ++i) {
  25.         //if(dest[i] == *value) return i;
  26.         if(operator_equ(&dest[i], value)) return i;
  27.     }
  28.     return -1;
  29. }

  30. size_t unique(struct tm_t *dest, const struct tm_t *src, size_t size) {
  31.     struct tm_t tmp[size]; memcpy(tmp, src, sizeof(*src) * size);
  32.     size_t new_size = 0;
  33.     for(size_t i = 0; i < size; ++i) {
  34.         if(find(dest, new_size, &tmp[i]) == -1) dest[new_size++] = tmp[i];
  35.     }
  36.     return new_size;
  37. }

  38. int main(void) {
  39.     size_t a, b, c;
  40.     scanf("%zu%*[+-*/=,.]%zu%*[+-*/=,.]%zu", &a, &b, &c);
  41.     const struct tm_t tms[6] = {{a, b, c}, {a, c, b}, {b, a, c}, {b, c, a}, {c, a, b}, {c, b, a}};
  42.     struct tm_t result[6]; size_t size = 0;
  43.     for(size_t i = 0; i < 6; ++i) {
  44.         if(check(&tms[i])) result[size++] = tms[i];
  45.     }
  46.     if(!size) {
  47.         printf("Invalid Date!\n");
  48.         return 0;
  49.     }
  50.     size = unique(result, result, size);
  51.     printf("%zu\n", size);
  52.     for(size_t i = 0; i < size; ++i) {
  53.         printf("%.04zu-%.02zu-%.02zu\n", result[i].year, result[i].month, result[i].day);
  54.     }
  55.     return 0;
  56. }
  57. sh-5.2$ ./main
  58. 2/3/1
  59. 6
  60. 0002-03-01
  61. 0002-01-03
  62. 0003-02-01
  63. 0003-01-02
  64. 0001-02-03
  65. 0001-03-02
  66. sh-5.2$ ./main
  67. 13+12+45
  68. 1
  69. 0045-12-13
  70. sh-5.2$ ./main
  71. 29,2,2019
  72. Invalid Date!
  73. sh-5.2$ ./main
  74. 35-36-37
  75. Invalid Date!
  76. sh-5.2$ ./main
  77. 11-30-31
  78. 1
  79. 0031-11-30
  80. sh-5.2$ ./main
  81. 5-5-13
  82. 2
  83. 0005-05-13
  84. 0013-05-05
  85. sh-5.2$ ./main
  86. 5-5-6
  87. 3
  88. 0005-05-06
  89. 0005-06-05
  90. 0006-05-05
  91. sh-5.2$ ./main
  92. 4-2-28
  93. 4
  94. 0004-02-28
  95. 0002-04-28
  96. 0028-04-02
  97. 0028-02-04
  98. sh-5.2$ ./main
  99. 5*5*5
  100. 1
  101. 0005-05-05
  102. sh-5.2$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-12 21:23:44 | 显示全部楼层
  1. import itertools

  2. def parse_date(input_date):
  3.     numbers = [int(num) for num in input_date if num.isdigit()]
  4.     permutations = set(itertools.permutations(numbers))
  5.     valid_dates = []
  6.     for perm in permutations:
  7.         year, month, day = perm
  8.         if 1 <= year <= 9999 and 1 <= month <= 12 and 1 <= day <= 31:
  9.             if month in [4, 6, 9, 11] and day > 30:
  10.                 continue
  11.             if month == 2 and day > 29:
  12.                 continue
  13.             if month == 2 and day == 29 and not (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
  14.                 continue
  15.             valid_dates.append((year, month, day))
  16.     if valid_dates:
  17.         valid_dates.sort(reverse=True)
  18.         print(len(valid_dates))
  19.         for date in valid_dates:
  20.             print(f"{date[0]:04d}-{date[1]:02d}-{date[2]:02d}")
  21.     else:
  22.         print("Invalid Date!")

  23. # 测试样例
  24. parse_date("2/3/1")
  25. parse_date("13+12+45")
  26. parse_date("29,2,2019")
  27. parse_date("35-36-37")
  28. parse_date("11-30-31")
  29. parse_date("5-5-13")
  30. parse_date("5-5-6")
  31. parse_date("4-2-28")
  32. parse_date("5*5*5")
复制代码

这段代码使用Python实现了根据输入的日期判断其是否可以理解为合法日期,并输出合法日期的个数以及按从大到小排序的合法日期。首先,它将输入的日期中的数字提取出来,并生成所有可能的排列组合。然后,对每种排列组合进行合法性判断,筛选出合法的日期并进行排序输出。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-11-12 21:24:19 | 显示全部楼层
isdkz 发表于 2023-11-12 21:23
这段代码使用Python实现了根据输入的日期判断其是否可以理解为合法日期,并输出合法日期的个数以及按从大 ...

用C语言,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-12 21:24:45 | 显示全部楼层
这个任务可以通过编写一个C程序来解决。你需要编写一个日期解析函数,该函数能够处理各种格式的日期输入,并输出所有可能的合法日期。

以下是一个简单的C程序示例,用于解析日期并输出结果。请注意,这只是一个示例,你可能需要根据实际需求进行修改和完善。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // 判断是否为闰年
  4. int isLeapYear(int year) {
  5.     return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  6. }
  7. // 解析日期
  8. void parseDate(char *input) {
  9.     int numbers[3];
  10.     char *ptr;
  11.     int count = 0;
  12.     // 解析输入
  13.     ptr = strtok(input, "-/+=*,.");  // 使用多种分隔符分割字符串
  14.     while (ptr != NULL) {
  15.         numbers[count] = atoi(ptr);
  16.         count++;
  17.         ptr = strtok(NULL, "-/+=*,.");
  18.     }
  19.     // 输出所有合法日期
  20.     printf("合法日期个数:\n");
  21.     // 进行日期判断与输出
  22.     // ... (此处需要根据日期规则逐一判断)
  23.     if (count == 3) {
  24.         // 输出解析后的数字
  25.         for (int i = count - 1; i >= 0; i--) {
  26.             printf("%04d-", numbers[i]);
  27.         }
  28.         printf("\n");
  29.     } else {
  30.         printf("Invalid Date!\n");
  31.     }
  32. }
  33. int main() {
  34.     char input[20];
  35.     printf("请输入日期:");
  36.     scanf("%s", input);
  37.     parseDate(input);
  38.     return 0;
  39. }
复制代码

这段代码中,parseDate 函数用于解析输入的日期,并输出所有可能的合法日期。你需要在其中添加逻辑以判断日期的合法性,并按要求输出结果。

在实际应用中,你可能还需要添加更多的错误处理和边界情况判断,以确保程序能够正确、稳定地运行。希望这个示例对你有所帮助,如果有其他问题,欢迎随时提问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-11-12 21:35:19 | 显示全部楼层
不回家
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-12 23:45:19 | 显示全部楼层
  1. bool check(const struct tm_t *tm) {
  2.     if(tm->year == 0) return false;
  3.     if(tm->month == 0) return false;
  4.     if(tm->day == 0) return false;
  5.     if(tm->month > 12) return false;
  6.     //size_t days[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  7.     size_t days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  8.     if(tm->day > days[tm->month]) return false;
  9.     //if(tm->month == 2 && tm->day == 29) return false;
  10.     return true;
  11. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 04:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表