What day is it

本文介绍了一种将特定日期转换为对应星期几的算法实现。通过分析不同月份天数及闰年规则,确保了日期的有效性,并利用公式计算出输入日期对应的星期。此算法适用于多种编程竞赛场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
Source
HDU 2007-11  Programming Contest_WarmUp

Submit

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int check_ord_day(int year, int month)
{
	int num;
	switch (month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		num = 31;
		break;
	case 2:
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
			return num = 29;
		else
			return num = 28;
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		num = 30;
		break;
	}
	return num;
}

int check(int year, int month, int day)
{
	if (year <= 0 || month <= 0 || month > 12 || day <= 0 || day > check_ord_day(year, month))
		return 1;
}
int week_change(int year, int month, int day)
{
	int s = 0;
	if (month == 1) { month = 13; year--; }
	if (month == 2) { month = 14; year--; }
	s = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
	return s;

}
char* s_change(int s)
{
	char *str;
	switch (s)
	{
	case 0:
		str = "Monday";
		break;
	case 1:
		str = "Tuesday";
		break;
	case 2:
		str = "Wednesday";
		break;
	case 3:
		str = "Thursday";
		break;
	case 4:
		str = "Friday";
		break;
	case 5:
		str = "Saturday";
		break;
	case 6:
		str = "Sunday";
		break;
	}
	return str;
}
int main()
{
	int year, month, day;
	while (scanf("%d%d%d", &year, &month, &day) != EOF)
	{
		if (check(year, month, day)==1)
		{
		    printf("illegal\n");

		}
		else
        {

            int s;
			s = week_change(year, month, day);

			char *ss;
			ss = s_change(s);
			printf("%s\n",ss);

        }
	}
	return 0;
}

Wrong Answer

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int d[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},
    {0,31,29,31,30,31,30,31,31,30,31,30,31}};
char c[7][10]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};


int check(int year,int month,int day)
{
    int i;
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
        i=1;
    else i=0;
    if(year<=0||month<=0||month>12||day<=0||day > d[i][month])
        return 1;
}

int week_change(int year,int month,int day)
{
    int s = 0;
    if (month == 1) { month = 13; year--; }
    if (month == 2) { month = 14; year--; }
    s = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
    return s;
}
int main()
{
    int year,month,day;
    while(scanf("%d%d%d", &year, &month, &day) != EOF)
    {
        if(check(year,month,day)==1)
            puts("illegal");
        else
        {
            int s;
            s=week_change(year,month,day);
            puts(c[s]);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值