poj 1082[博弈,日历处理,stack overflow错误]

Calendar Game
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3698 Accepted: 1671

Description

Adam and Eve enter this year's ACM International Collegiate Programming Contest. Last night, they played the Calendar Game, in celebration of this contest. This game consists of the dates from January 1, 1900 to November 4, 2001, the contest day. The game starts by randomly choosing a date from this interval. Then, the players, Adam and Eve, make moves in their turn with Adam moving first: Adam, Eve, Adam, Eve, etc. There is only one rule for moves and it is simple: from a current date, a player in his/her turn can move either to the next calendar date or the same day of the next month. When the next month does not have the same day, the player moves only to the next calendar date. For example, from December 19, 1924, you can move either to December 20, 1924, the next calendar date, or January 19, 1925, the same day of the next month. From January 31 2001, however, you can move only to February 1, 2001, because February 31, 2001 is invalid. 

A player wins the game when he/she exactly reaches the date of November 4, 2001. If a player moves to a date after November 4, 2001, he/she looses the game. 

Write a program that decides whether, given an initial date, Adam, the first mover, has a winning strategy. 

For this game, you need to identify leap years, where February has 29 days. In the Gregorian calendar, leap years occur in years exactly divisible by four. So, 1993, 1994, and 1995 are not leap years, while 1992 and 1996 are leap years. Additionally, the years ending with 00 are leap years only if they are divisible by 400. So, 1700, 1800, 1900, 2100, and 2200 are not leap years, while 1600, 2000, and 2400 are leap years.

Input

The input consists of T test cases. The number of test cases (T ) is given in the first line of the input file. Each test case is written in a line and corresponds to an initial date. The three integers in a line, YYYY MM DD, represent the date of the DD-th day of MM-th month in the year of YYYY. Remember that initial dates are randomly chosen from the interval between January 1, 1900 and November 4, 2001.

Output

Print exactly one line for each test case. The line should contain the answer "YES" or "NO" to the question of whether Adam has a winning strategy against Eve. Since we have T test cases, your program should output totally T lines of "YES" or "NO".

Sample Input

3 
2001 11 3 
2001 11 2 
2001 10 3 

Sample Output

YES
NO
NO

Source





/*
关键的是
1、日历处理上,就是闰年的2月要特别处理。
2、SG函数的计算,刚开始用记忆化(意思就是如果算过了就不算,否则递归地算)算,这样会stack overflow(因为如果一开始就算1900 1 1,那递归的结果就是会接着算1900 1 2,把1900 1 1放在stack中,然后1900 1 2又会接着算1900 1 3,如此这般,最后的出口是2001 11 4,大概要365*(2011-1900)即30000多次,即把数据保存在stack中30000多次,然后就stack overflow了,实际上当这样递归到达4800次stack就已经overflow了,测试程序见下)。
这个程序在VS2008下出现错误,在VC6.0下说是stack overflow。
3、知道了原因,1900 1 1 不行改用从2001 11 3号开始从后往前算,就是把1900 1 1 到2001 11 3全部计算一遍时间复杂度大概是365*(2011-1900)即30000多(当然还要乘上一个常数,了不起就是10),完全可以承受。
#include<iostream>
using namespace std;

int f(int i)
{
return i*2/2+4-4;
}

int f2(int k)
{
if(k==0) return 1;
f(k);
f2(k-1);
return 0;
}

int main()
{
f2(4800);
return 0;
}
*/










#include<iostream>
using namespace std;

int mm[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int sg[2001-1900+10][13][32];

inline bool is_leap_year(int y)
{
	return ((y%4==0)&&(y%100!=0))||(y%400==0);
}

inline bool valid_day(int &y,int &m,int &d)
{
	return y*366+m*31+d<=2001*366+11*31+4;
}

bool next_day(int &y,int &m,int &d)
{
	int i,j,k;
	if(m==12&&d==31)//next year;
	{
		y++;
		m=d=1;
		return valid_day(y,m,d);
	}
	//next month;
	if(is_leap_year(y)&&m==2&&d==28)
	{
		d++;
		return valid_day(y,m,d);
	}
	if(is_leap_year(y)&&m==2&&d==29)
	{
		d=1;
		m=3;
		return valid_day(y,m,d);
	}
	if(mm[m]==d)
	{
		m++;
		d=1;
		return valid_day(y,m,d);
	}

	//next day;
	d++;
//	cout<<"come here"<<endl;
	return valid_day(y,m,d);
}

bool next_month(int &y,int &m,int &d)
{
	int i,j,k;
	//next year;
	if(m==12)
	{
		y++;
		m=1;
		return valid_day(y,m,d);
	}
	if(is_leap_year(y)&&m==1)
	{
		if(d<=29) 
		{
			m++;
			return valid_day(y,m,d);
		}
		return 0;
	}
	
	m++;
	if(d<=mm[m]) return valid_day(y,m,d);
	else return 0;
}

int SG(int y,int m,int d)
{
	if(sg[y-1900][m][d]!=-1) return sg[y-1900][m][d];

	int next_y=y,next_m=m,next_d=d;
	//hava a valid next day
/*
	if(next_day(next_y,next_m,next_d)) cout<<"have next day"<<endl;
	else cout<<"haven't next day"<<endl;

	
	cout<<"next day is"<<next_y<<' '<<next_m<<' '<<next_d<<endl;
	//return 0;
	
*/
	if(next_day(next_y,next_m,next_d)==1)
	{
		if(SG(next_y,next_m,next_d)==0)
			return sg[y-1900][m][d]=1;
	}
	//have a valid next month
	next_y=y;next_m=m;next_d=d;
/*
	if(next_month(next_y,next_m,next_d)) cout<<"have next day"<<endl;
	else cout<<"haven't next day"<<endl;

	
	cout<<"next day is"<<next_y<<' '<<next_m<<' '<<next_d<<endl;
	return 0;
*/
	if(next_month(next_y,next_m,next_d)==1)
	{
		if(SG(next_y,next_m,next_d)==0)
			return sg[y-1900][m][d]=1;
	}

	return sg[y-1900][m][d]=0;
}

int main()
{
	int i,j,k,n;
	int y,m,d;
	memset(sg,-1,sizeof(sg));
	sg[2001-1900][11][4]=0;

	for(i=2001;i>=1900;i--)//把2001 11 4到1900 1 1全算一遍,由于逆序,不会stack overflow,而且时间复杂度完全可以承受。
		for(j=12;j>=1;j--)
			for(k=31;k>=1;k--)
				if(valid_day(i,m,d))
					SG(i,j,k);//记忆化
	cin>>n;
	for(i=0;i<n;i++)
	{
		scanf("%d%d%d",&y,&m,&d);
		SG(y,m,d)?puts("YES"):puts("NO");
	}


	return 0;
}


内容概要:本文档是一份计算机软考初级程序员的经典面试题汇编,涵盖了面向对象编程的四大特征(抽象、继承、封装、多态),并详细探讨了Java编程中的诸多核心概念,如基本数据类型与引用类型的区别、String和StringBuffer的差异、异常处理机制、Servlet的生命周期及其与CGI的区别、集合框架中ArrayList、Vector和LinkedList的特性对比、EJB的实现技术及其不同Bean类型的区别、Collection和Collections的差异、final、finally和finalize的作用、线程同步与异步的区别、抽象类和接口的区别、垃圾回收机制、JSP和Servlet的工作原理及其异同等。此外,还介绍了WebLogic服务器的相关配置、EJB的激活机制、J2EE平台的构成和服务、常见的设计模式(如工厂模式)、Web容器和EJB容器的功能、JNDI、JMS、JTA等J2EE核心技术的概念。 适合人群:正在备考计算机软考初级程序员的考生,或希望加深对Java编程及Web开发理解的初、中级开发人员。 使用场景及目标:①帮助考生系统复习Java编程语言的基础知识和高级特性;②为实际项目开发提供理论指导,提升编程技能;③为面试准备提供参考,帮助求职者更好地应对技术面试。 其他说明:文档不仅涉及Java编程语言的核心知识点,还包括了Web开发、企业级应用开发等方面的技术要点,旨在全面提高读者的专业素养和技术水平。文档内容详实,适合有一定编程基础的学习者深入学习和研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值