鱼C论坛

 找回密码
 立即注册
查看: 654|回复: 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
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$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-12 21:23:34 | 显示全部楼层    本楼为最佳答案   
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$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

# 测试样例
parse_date("2/3/1")
parse_date("13+12+45")
parse_date("29,2,2019")
parse_date("35-36-37")
parse_date("11-30-31")
parse_date("5-5-13")
parse_date("5-5-6")
parse_date("4-2-28")
parse_date("5*5*5")
这段代码使用Python实现了根据输入的日期判断其是否可以理解为合法日期,并输出合法日期的个数以及按从大到小排序的合法日期。首先,它将输入的日期中的数字提取出来,并生成所有可能的排列组合。然后,对每种排列组合进行合法性判断,筛选出合法的日期并进行排序输出。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

用C语言,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

以下是一个简单的C程序示例,用于解析日期并输出结果。请注意,这只是一个示例,你可能需要根据实际需求进行修改和完善。
#include <stdio.h>#include <stdlib.h>// 判断是否为闰年int isLeapYear(int year) {    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);}// 解析日期void parseDate(char *input) {    int numbers[3];    char *ptr;    int count = 0;    // 解析输入    ptr = strtok(input, "-/+=*,.");  // 使用多种分隔符分割字符串    while (ptr != NULL) {        numbers[count] = atoi(ptr);        count++;        ptr = strtok(NULL, "-/+=*,.");    }    // 输出所有合法日期    printf("合法日期个数:\n");    // 进行日期判断与输出    // ... (此处需要根据日期规则逐一判断)    if (count == 3) {        // 输出解析后的数字        for (int i = count - 1; i >= 0; i--) {            printf("%04d-", numbers[i]);        }        printf("\n");    } else {        printf("Invalid Date!\n");    }}int main() {    char input[20];    printf("请输入日期:");    scanf("%s", input);    parseDate(input);    return 0;}
这段代码中,parseDate 函数用于解析输入的日期,并输出所有可能的合法日期。你需要在其中添加逻辑以判断日期的合法性,并按要求输出结果。

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

使用道具 举报

 楼主| 发表于 2023-11-12 21:35:19 | 显示全部楼层
不回家
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-12 23:45:19 | 显示全部楼层
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};
    size_t days[13] = {0, 31, 28, 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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-7-27 15:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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