早川604 2023-01-01 22:47 采纳率: 100%
浏览 44
已结题

关于#c语言#的问题,如何解决?

7-5 2908 算日期
分数 100

今天是2024年5月18日,给定今年的某一个日期,输出该日期与今天相隔多少天?

输入格式:
输入有多组,每组为一个日期,格式是mm/dd,如10/01。

输出格式:
每行一个数,表示5月18日与这一天相隔的天数。

输入样例:
10/01
06/01
输出样例:
136
14
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB

  • 写回答

3条回答 默认 最新

  • m0_57781768 2023-01-02 00:01
    关注

    回答不易,求求您点赞关注哦

    这是一个 C 语言的解决方案,它将格式为“mm/dd”的日期作为输入,并输出该日期与 2024 年 5 月 18 日之间的天数:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // Returns the number of days in a month
    int days_in_month(int month) {
      if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
        return 31;
      } else if (month == 4 || month == 6 || month == 9 || month == 11) {
        return 30;
      } else if (month == 2) {
        return 28;
      } else {
        printf("Invalid month\n");
        exit(1);
      }
    }
    
    int main(void) {
      char date[5]; // input date in the format "mm/dd"
      int month, day;
    
      while (scanf("%s", date) == 1) {
        // Extract month and day from the input string
        month = atoi(strtok(date, "/"));
        day = atoi(strtok(NULL, "/"));
    
        // Check if the input date is valid
        if (month < 1 || month > 12 || day < 1 || day > days_in_month(month)) {
          printf("Invalid date\n");
          continue;
        }
    
        // Calculate the number of days between the input date and May 18, 2024
        int days = 0;
        for (int i = 5; i < month; i++) {
          days += days_in_month(i);
        }
        days += day - 18;
    
        printf("%d\n", days);
      }
    
      return 0;
    }
    

    您可以使用 C 编译器(例如 GCC)编译和运行该程序。例如:

    gcc -o days days.c
    ./days
    

    然后,您可以输入要检查的日期,每行一个,程序将输出每个日期与 2024 年 5 月 18 日之间的天数。

    例如:

    $ ./days
    10/01
    06/01
    136
    14
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月11日
  • 已采纳回答 1月3日
  • 创建了问题 1月1日

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据