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

Project Euler Problem 20

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

desi lydic,孙凯南,武汉理工大学研究生复试分数线

n! means n × (n − 1) × ... × 3 × 2 × 1

For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

Find the sum of the digits in the number 100! 标签: <无>

代码片段(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=20;//Problem Order

var problem='n! means n x (n - 1) x ... x 3 x 2 x 1<br/>\
For example, 10! = 10 x 9 x ... x 3 x 2 x 1 = 3628800,<br/>\
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.<br/>\
Find the sum of the digits in the number 100!';


//solve the problem
//==============编程思路简介================
//	用数组存储n的阶乘的每一位,
//		通过动态规划来逐步求100的阶乘
//=========================================
var fact=[4,2];

for(var n=5;n<101;n++){
	var jin_wei=0;
	for(var j=0;j<fact.length;j++){
		var tmp=fact[j]*n+jin_wei;
		fact[j]=tmp%10;
		jin_wei=parseInt(tmp/10);
	}
	while(jin_wei>0){
		fact.push(jin_wei%10);
		jin_wei=parseInt(jin_wei/10);
	}
}
var sum=0;//存储100!的所有数字之和
for(var i=0;i<fact.length;i++)sum+=fact[i];
//update browser
document.getElementById("problemNum").innerHTML="Project Euler-Problem "+p_order;
document.getElementById("problemContent").innerHTML=problem;
document.getElementById("sum").innerHTML="Answer:"+sum;
document.getElementById("copyleft").innerHTML="CopyLeft@Thrombin 2015";
</script>
</body>
</html>

grunt less自动编译,css,js压缩

如下是一些相关的URL链接http://www.gruntjs.net/plugins(grunt插件列表)http://developer.51cto.com/art/201506/479127.

Tags标签ltgt10trtd

热门推荐