编程介的小学生 2019-06-14 21:49 采纳率: 20.5%
浏览 581

日期的推算来计算是星期几的问题,要求使用C语言的程序的代码编程的方式如何实现的呢?

Problem Description
Today is Saturday, 17th Nov,2007. Now, if i tell you a date, can you tell me what day it is ?

Input
There are multiply cases.
One line is one case.
There are three integers, year(0<year<10000), month(0<=month<13), day(0<=day<32).

Output
Output one line.
if the date is illegal, you should output "illegal". Or, you should output what day it is.

Sample Input
2007 11 17

Sample Output
Saturday

  • 写回答

1条回答 默认 最新

  • Jacky Yuan 2019-06-15 14:07
    关注

    使用基姆拉尔森计算公式吧,公事如下:
    W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7

    在公式中d表示日期中的日数,m表示月份数,y表示年数。

    注意:在公式中有个与其他公式不同的地方:

    把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
    以公元元年为参考,公元元年1月1日为星期一

    基姆拉尔森计算公式例程:

    #include <stdio.h>
    
    const char * getWeekdayByYearday(int iY, int iM, int iD) 
    {
        int iWeekDay = -1; 
        if (1 == iM || 2 == iM) 
        {   
            iM += 12; 
            iY--;
        }   
        iWeekDay = (iD + 1 + 2 * iM + 3 * (iM + 1) / 5 + iY + iY / 4 - iY / 100 + iY / 400) % 7;
        switch(iWeekDay)
        {   
            case 0 : return "Sunday"; break;
            case 1 : return "Monday"; break;
            case 2 : return "Tuesday"; break;
            case 3 : return "Wednesday"; break;
            case 4 : return "Thursday"; break;
            case 5 : return "Friday"; break;                                                             
            case 6 : return "Saturday"; break;
            default : return NULL; break;
        }   
    
        return NULL;
    }
    
    int main()
    {
        int year,month,day;
        char ch='1';
        while(ch != '\033')
        { 
            printf("\n请输入日期:\n格式为:1900,1,1\n");
            scanf("%d,%d,%d",&year,&month,&day);
            const char * p = getWeekdayByYearday(year, month, day);
            printf("WeekDay : %s\n", p);
            ch = getchar();
            printf("\n");
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog