Last active
August 29, 2015 14:12
-
-
Save nothingrealhappen/caa2c1ac6e706f81cb00 to your computer and use it in GitHub Desktop.
「Geek每日一题」2014.12.30
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Come on ! |
def calculate count, year, adult, array
if year == 0
count
else
calculate(count+adult, year-1, adult + array[0], [array[1], array[2], adult])
end
end
没验证的测试用例你出个屁题~
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>算算有几只乌龟</title>
</head>
<body>
请输入你想计算的年份:
<input type="number">
<button id="calc">计算</button>
<div class="result" style="display: none;">
第<span id="year"></span>有<span id="nums"></span>只乌龟
</div>
<script>
window.onload = function() {
var btn = document.querySelector('#calc'),
n,
tor;
btn.addEventListener('click', function() {
n = document.querySelector('input').value;
n = n || 1;
tor = countTor(Number(n));
document.querySelector('#year').innerHTML = n;
document.querySelector('#nums').innerHTML = tor.nums;
document.querySelector('.result').style.display = 'block';
});
};
// 计算乌龟数量的主要算法
function countTor(n) {
var t = {};
if(n == 1) {
t.tor1 = 1;
t.tor2 = 0;
t.tor3 = 0;
t.adult = 1;
} else {
var lt = arguments.callee(n - 1);
t.tor1 = lt.adult;
t.tor2 = lt.tor1;
t.tor3 = lt.tor2;
t.adult = lt.adult + lt.tor3;
}
t.nums = t.adult + t.tor1 + t.tor2 + t.tor3;
return t;
};
</script>
</body>
</html>
def count (n, sum=0, adult=1, born=[0, 0, 0])
return sum if n < 0
count(n - 1, sum + adult, adult + born[0], [born[1], born[2], adult])
end
puts count(100)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
有一只母乌龟 ,它每年年初生一头小母乌龟 。每头小母乌龟 从第四个年头开始,每年年初生一头小母乌龟 。请你计算第n年是共有多少只母乌龟 (第一年是有一头母乌龟)