编程介的小学生 2019-05-28 00:02 采纳率: 20.5%
浏览 770

输入一个日期计算是星期几的问题,怎么利用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条回答 默认 最新

  • 张兴华(MarsXH.Chang) 前端领域新星创作者 2019-05-28 10:14
    关注
    #include "stdio.h"
    main () {
        int y,m,d,a,b;
        b = 0;
        scanf("%d%d%d", &y, &m, &d);
        if (m > 12 || m < 0) {
            b = 1;
            printf("month is error.\n");
        } else if (d < 0) {
            b = 1;
            printf("day is error.\n");
        } else {
            switch(m) {
                case 1||3||5||7||8||10||12:
                    if (d > 32) {
                        b = 1;
                        printf("day is error.\n");
                    }; 
                    break;
                case 4||6||9||11:
                    if (d > 31) {
                        b = 1;
                        printf("day is error.\n");
                    }; 
                    break;
                case 2:
                    if((y%4==0 && y%100!=0)||(y%400==0)) a=1;
                    else a=2;
                    if(a==1&&d>30) {b = 1; printf("day is error.\n");}; break;
                    if(a==2&&d>29) {b = 1; printf("day is error.\n");}; break;
            }
        }
        if (m == 1 || m == 2) {
            m+=12;
            y--;
        }
        if(b == 0) {
            int iWeek = (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
            switch(iWeek) {
                case 0: printf("1\n"); break;
                case 1: printf("2\n"); break;
                case 2: printf("3\n"); break;
                case 3: printf("4\n"); break;
                case 4: printf("5\n"); break;
                case 5: printf("6\n"); break;
                case 6: printf("0\n"); break;
            }
        }
        system("pause");
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?