|
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语言,要输出有几种,然后按从大到小排序
- sh-5.2$ cat main.c
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- struct tm_t {size_t year, month, day;};
- bool check(const struct tm_t *tm) {
- if(tm->year == 0) return false;
- if(tm->month == 0) return false;
- if(tm->day == 0) return false;
- if(tm->month > 12) return false;
- size_t days[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- if(tm->day > days[tm->month]) return false;
- if(tm->month == 2 && tm->day == 29) return false;
- return true;
- }
- //bool operator==(const struct tm_t *a, const struct tm_t *b) {
- bool operator_equ(const struct tm_t *a, const struct tm_t *b) {
- if(a->year != b->year) return false;
- if(a->month != b->month) return false;
- if(a->day != b->day) return false;
- return true;
- }
- size_t find(const struct tm_t *dest, size_t size, const struct tm_t *value) {
- for(size_t i = 0; i < size; ++i) {
- //if(dest[i] == *value) return i;
- if(operator_equ(&dest[i], value)) return i;
- }
- return -1;
- }
- size_t unique(struct tm_t *dest, const struct tm_t *src, size_t size) {
- struct tm_t tmp[size]; memcpy(tmp, src, sizeof(*src) * size);
- size_t new_size = 0;
- for(size_t i = 0; i < size; ++i) {
- if(find(dest, new_size, &tmp[i]) == -1) dest[new_size++] = tmp[i];
- }
- return new_size;
- }
- int main(void) {
- size_t a, b, c;
- scanf("%zu%*[+-*/=,.]%zu%*[+-*/=,.]%zu", &a, &b, &c);
- const struct tm_t tms[6] = {{a, b, c}, {a, c, b}, {b, a, c}, {b, c, a}, {c, a, b}, {c, b, a}};
- struct tm_t result[6]; size_t size = 0;
- for(size_t i = 0; i < 6; ++i) {
- if(check(&tms[i])) result[size++] = tms[i];
- }
- if(!size) {
- printf("Invalid Date!\n");
- return 0;
- }
- size = unique(result, result, size);
- printf("%zu\n", size);
- for(size_t i = 0; i < size; ++i) {
- printf("%.04zu-%.02zu-%.02zu\n", result[i].year, result[i].month, result[i].day);
- }
- return 0;
- }
- sh-5.2$ ./main
- 2/3/1
- 6
- 0002-03-01
- 0002-01-03
- 0003-02-01
- 0003-01-02
- 0001-02-03
- 0001-03-02
- sh-5.2$ ./main
- 13+12+45
- 1
- 0045-12-13
- sh-5.2$ ./main
- 29,2,2019
- Invalid Date!
- sh-5.2$ ./main
- 35-36-37
- Invalid Date!
- sh-5.2$ ./main
- 11-30-31
- 1
- 0031-11-30
- sh-5.2$ ./main
- 5-5-13
- 2
- 0005-05-13
- 0013-05-05
- sh-5.2$ ./main
- 5-5-6
- 3
- 0005-05-06
- 0005-06-05
- 0006-05-05
- sh-5.2$ ./main
- 4-2-28
- 4
- 0004-02-28
- 0002-04-28
- 0028-04-02
- 0028-02-04
- sh-5.2$ ./main
- 5*5*5
- 1
- 0005-05-05
- sh-5.2$
复制代码
|
|