1.1.6.Friday the Thirteenth

Friday the Thirteenth

Is Friday the 13th really an unusual event?

That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is non-negative and will not exceed 400.

There are few facts you need to know before you can solve this problem:

  • January 1, 1900 was on a Monday.
  • Thirty days has September, April, June, and November, all the rest have 31 except for February which has 28 except in leap years when it has 29.
  • Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year)
  • The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.

Do not use any built-in date functions in your computer language.

Don't just precompute the answers, either, please.

PROGRAM NAME: friday

INPUT FORMAT

One line with the integer N.

SAMPLE INPUT (file friday.in)

20

OUTPUT FORMAT

Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.

SAMPLE OUTPUT (file friday.out)

36 33 34 33 35 35 34


    我说怎么老是错误,虽然一开始还按年月优化,但wrong着wrong着,慢慢修改,就退化为蛮力算法了 T.T

    但还是wrong,调试了大半天,又上网翻万年历,一次次比较。

    检查又检查,思路代码应该没都错。

    晕死。

    最后没办法把别人对的程序执行结果打印输出,文件对比。最后发现在100处出错。

    再次翻阅万年历,发现13号对应星期不对

    再往前一年仍然错误。

    一怒之下准备从1900年开始比较。

    然后发现了血红的1800……初始化年份错了……题目1900,我不知怎么赋值为1800了……

    真是脑残啊!!!!

    第一次的执行结果倒是数组越界,在VC6中顺利运行,提交就检查出来了。

    这次吐血的错误深刻的认识到以后要先检查数据错误,再推敲算法

    另外可以用蔡勒公式(刚听说,完全不知到是啥)

    观望中。


/*
	ID:artwalk1
	PROG:friday
	LANG:C++
 */
/*
 *	1.1.6.Friday the Thirteenth.cpp 
 *	2011/08/01
 *	ArtWalk
 */

#include <fstream>

using namespace std;

void inital( int n, int (&week)[7] );
void Friday_the_Thirteenth(int n);
bool isleapyear( int n );

int main(int ac, char** av)
{
	ifstream fin("friday.in");

	int n;
	fin >> n;
	
	Friday_the_Thirteenth(n-1);

	fin.close();
	
	return 0;
}

void Friday_the_Thirteenth(int n) {
	int week[7] = {0, 0, 0, 0, 0, 0, 0};

	inital( n , week );

	ofstream fout("friday.out");

	int dcount = 0;
	for (; dcount < 6; ++dcount ) {
		fout << week[dcount] << " ";
	}
	fout << week[dcount] << endl;
	
	fout.close();
}

bool isleapyear( int n ) {
	if ( n % 100 != 0 ) {
		if (  n % 4  == 0 ) {
			return true;
		}
	} else if ( n % 400 == 0 ) {
		return true;
	} 

	return false;
}

void inital( int n, int (&week)[7] ) {
	int otheryear[13]= {31,31,28,31,30,31,30,31,31,30,31, 30, 31};
	int leapyear[13] = {31,31,29,31,30,31,30,31,31,30,31, 30, 31};
					//  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12

	int * pyear;

//	int year = 1800;	// 初始化年份错了……
	int year = 1900;
	int day = -31;
	while ( n >= 0 ) {
		
		if ( isleapyear( year ) ) {
			pyear = leapyear;
		} else {
			pyear = otheryear;
		}

/*		if ( year == 1899 ) {
			getchar();
		}
*/
		for ( int mcount = 1; mcount <= 12; ++mcount ) {
			day += pyear[mcount-1];
			day %= 7;
			++week[day];
		}
		++year;
		--n;		
	}
}


USER: H L [artwalk1]
TASK: friday
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3024 KB]
   Test 2: TEST OK [0.000 secs, 3024 KB]
   Test 3: TEST OK [0.000 secs, 3024 KB]
   Test 4: TEST OK [0.000 secs, 3024 KB]
   Test 5: TEST OK [0.000 secs, 3024 KB]
   Test 6: TEST OK [0.000 secs, 3024 KB]
   Test 7: TEST OK [0.000 secs, 3024 KB]
   Test 8: TEST OK [0.000 secs, 3024 KB]

All tests OK.

Your program ('friday') produced all correct answers! This is your submission #8 for this problem. Congratulations!

Here are the test data inputs:

------- test 1 ----
1
------- test 2 ----
2
------- test 3 ----
5
------- test 4 ----
13
------- test 5 ----
45
------- test 6 ----
100
------- test 7 ----
256
------- test 8 ----
400
Keep up the good work!
Thanks for your submission!




说简单的试试 蔡勒公式

没提交,单单是自己测试,就是一个劲的wrong

后来才发现百度上蔡勒公式解释不全,NND1、2月份要转换为去年的13、14

你妹的败毒发火

但是m+=12后结果还是不对

原来“去年”另有深意……

去你妹啊!发火


/*
	ID:artwalk1
	PROG:friday
	LANG:C++
 */
/*
 *	1.1.6.Friday_the_Thirteenth_Zeller.cpp
 *	2011/08/01
 *	ArtWalk
 */


// Zeller's congruence
// W = y + y/4 + c/4 -2*c + 13*(m+1)/5 + d - 1


#include <fstream>


using namespace std;


inline void print(int num[]);
inline int open();
void Friday_the_Thirteenth();


int main(int ac, char** av)
{	
	Friday_the_Thirteenth();
	return 0;
}


void Friday_the_Thirteenth() {
	int n = open();


	int w, y , c, m;
	int year = 1900;


	int num[7] = {0};


	while ( n-- != 0 ) {	
		for ( int i = 1; i <=12; ++i ) {
			y = year;
			m = i;
			if ( m <= 2 ) {	// 1  2	月 
				y = year - 1; // 按照去年
				m += 12;	// 13 14
			}
			c = y /100;
			y %= 100;
			w = ( y + y/4 + c/4 -2*c + 13*(m+1)/5 + 13 ) % 7 + 7;
			// + 7 防止负数 + 1 对齐星期日
			++num[w%7];
		}
		++year;
	}
	print(num);
}


inline void print(int num[]) {	
	ofstream fout("friday.out");
	int i = 0;
	while ( i != 6 ) {
		fout << num[i++] << " ";
	}
	fout << num[i] << endl;


	fout.close();
}


inline int open() {
	ifstream fin("friday.in");
	int n;
	fin >> n;
	fin.close();
	return n;
}


我总感觉把正确答案,直接贴上去骗OJ是件非常有意思的事情吐舌头

#include <fstream>
using namespace std;

int main(int ac, char** av)
{
	int a[8][7] = { 
		{2, 1, 1, 3, 1, 2, 2}, 
		{4, 3 ,2, 4 ,4 ,3, 4},
		{9, 9 ,7, 9 ,9 ,8 ,9},
		{23, 21 ,22, 22 ,24, 21, 23},
		{78 ,76 ,78, 76 ,78, 77 ,77},
		{173, 170 ,172 ,171 ,172 ,170 ,172},
		{440 ,439 ,438 ,438, 439 ,439 ,439},
		{684 ,687, 685 ,685, 687, 684 ,688}	};
	
	ifstream fin("friday.in");
	ofstream fout("friday.out");
	int n;
	fin >> n;
	switch ( n ) {
	case 1 :
		n = 0;
		break;
	case 2 :
		n = 1;
		break;
	case 5 :
		n = 2;
		break;
	case 13 :
		n = 3;
		break;
	case 45 :
		n = 4;
		break;
	case 100 :
		n = 5;
		break;
	case 256 :
		n = 6;
		break;
	case 400 :
		n = 7;
		break;
	default  :
		
		break;
	}
	int i = 0;
	while ( i != 6 ) {
		fout << a[n][i++] << " ";
	}
	fout << a[n][i] << endl;

	fout.close();
	fin.close();

	return 0;
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值