An problem about date(怎么就放在动态规划里面呢。。。)

An problem about date

时间限制: 2000 ms | 内存限制: 65535 KB
难度: 2
描述

acm的iphxer经常忘记某天是星期几,但是他记那天的具体日期,他希望你能写个程序帮帮他。

输入
每行有三个整数 year,month,day,日期在1600年1月1日到9600年1月1日之间;
输出
输出对应的星期,用一个整数表示;(星期一到星期六用1-6表示,星期日用0表示)
样例输入
2011 3 6
1949 10 1
2011 4 1
1945 8 15
样例输出
0
6
5
3
my answer:
// 方法一
 
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<stdlib.h>

// 判断是否是闰年 
using namespace std;
bool run(int y)
{
	if(y%400 == 0 || y%4==0&&y%100 != 0) return true;
	else return false;
}
int a[12000];     // 打表 存放每年的1 月1日是星期几 
int b[13]={0,31,0,31,30,31,30,31,31,30,31,30,31};  // 月份天数 
int main()
{
	a[3615]=4;
	// before 2015
	for(int i = 3614; i >=3000; i --)
	{
		a[i]=a[i+1]-365%7;
		if(run(i-1600)) a[i]--;
		if(a[i]<=0) a[i]=7+a[i];
	}
	// after 2015
	for(int i =3616 ; i <= 12000; i ++)
	{
		 a[i]=365%7+a[i-1];
		 if(run(i-1600-1))  a[i]++;
		 if(a[i]==8)
		    a[i]=1; 
	}
	int year, month,day;
	while(scanf("%d%d%d",&year,&month,&day)==3)
	{
		
		int countday = day-1, data = 0;
		if(month > 3)
		 {
			for(int i = 1; i <month; i ++)
				countday+=b[i];
			if(run(year)) countday+=29;
			else countday+=28;
		}else {
			for(int i = 1; i < month; i ++)
			   countday+=b[i];
		}
		data = a[year+1600]+countday%7;
	    printf("%d\n",data%7);
	}
	return 0;
}

// 方法二 基姆拉尔森计算公式 ,竟然有公式<img alt="尴尬" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif" />!!!!
/*

 #include<iostream>
 #include<stdio.h>
 using namespace std;
 int weekday[]={0,1,2,3,4,5,6};
 int main()
 {
 	int year,month,day;
 	while(scanf("%d%d%d",&year,&month,&day)==3)
 	{
 		if(month == 1 || month == 2)
 		{
 			 month=(month == 1?13:14);
			 year--;
 		}
		int data=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400+1)%7;
		printf("%d\n",data);
 	}
 
 	return 0;
 }
 */


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值