学生教材网 >程序设计 > 代码分享 > JavaScript > 浏览文章

Project Euler Problem 19

来源:网络编辑:佚名时间:2015-12-20人气:

李三弟,黑夜卡盟,繁昌教育信息网

You are given the following information, but you may prefer to do some research for yourself.

1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? 标签: <无>

代码片段(1) [全屏查看所有代码]

1. [代码][JavaScript]代码     跳至 [1] [全屏预览]

<html>
<head>

</head>
<body style="text-align=center;font-size:32px;">
<table align="center">
<tr><td><div id="problemNum" style='background-color:#999999;width:800;text-align:center;font-size:32px;'></div></td></tr>
<tr><td><div id="problemContent" style='word-wrap:break-word;background-color:#bbbbbb;width:800;text-align:left;font-size:Npx;'></div></td></tr>
<tr><td><div id="sum" style='word-wrap:break-word; color:#ffff22;font-size:48;background-color:#8855ff;width:800;text-align:center;'></div></td></tr>
<tr><td><div id="copyleft" style='word-wrap:break-word; color:#ffff22;font-size:18;background-color:#666666;width:800;text-align:right;'></div></td></tr>
<script language="javascript">
	//---------------------------------//
	// Project Euler 
	//
	// Author:thrombin
	//   Date:2015-12-19
	//---------------------------------//  
var p_order=19;//Problem Order

var problem='You are given the following information, but you may prefer to do some research for yourself.<br/>\
1 Jan 1900 was a Monday.<br/>\
Thirty days has September,<br/>\
April, June and November.<br/>\
All the rest have thirty-one,<br/>\
Saving February alone,<br/>\
Which has twenty-eight, rain or shine.<br/>\
And on leap years, twenty-nine.<br/>\
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.<br/>\
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?';


//solve the problem
//==============编程思路简介================
//	扫描所有的日子,判断相比与1900年1月2日是否间隔天数是否为7的整数倍
//					同时判断当天是否为月初一号
//				同时满足以上两个条件计数值加一
//=====================================
var monthDays=[31,28,31,30,31,30,31,31,30,31,30,31];
var leapYear=false;//用于保存当前年份是否为闰年
var days=1;//用于保存从1900年1月2日开始经过的天数
var result=0;//用于保存同时是周日和月初第一天的次数

for(var year=1900;year<2001;year++){
	if((year%4==0 && year%100!=0) ||(year%400==0))leapYear=true;
	else{leapYear=false;}
	for(var month=0;month<12;month++){
		if(days%7==0 && year>1900)result++;
		days+=monthDays[month];
		if(leapYear && month==1)days++;
	}
}

//update browser
document.getElementById("problemNum").innerHTML="Project Euler-Problem "+p_order;
document.getElementById("problemContent").innerHTML=problem;
document.getElementById("sum").innerHTML="Answer:"+result;
document.getElementById("copyleft").innerHTML="CopyLeft@Thrombin 2015";
</script>
</body>
</html>

Project Euler Problem 15

Startinginthetopleftcornerofa2×2grid,andonlybeingabletomovetotherightanddown,&nb…

Project Euler Problem 19

Youaregiventhefollowinginformation,butyoumayprefertodosomeresearchforyourself.

Tags标签ltgt31brtr

热门推荐