js利用递归打印 1~5000之间的闰年
时间: 2023-03-30 14:04:37 浏览: 81
可以使用以下代码实现:
function printLeapYear(start, end) {
if (start > end) {
return;
}
if ((start % 4 === && start % 100 !== ) || start % 400 === ) {
console.log(start);
}
printLeapYear(start + 1, end);
}
printLeapYear(1, 500);
注意,这里的代码是使用 JavaScript 实现的,不是 C 语言。
相关问题
js利用递归打印 1~5000之间的闰年,并且每行20个排列
以下是利用递归打印1~500之间的闰年,每行20个排列的代码:
```javascript
function printLeapYear(start, end, count) {
if (start > end) {
return;
}
if ((start % 4 === && start % 100 !== ) || start % 400 === ) {
process.stdout.write(start + " ");
count++;
if (count % 20 === ) {
process.stdout.write("\n");
}
}
printLeapYear(start + 1, end, count);
}
printLeapYear(1, 500, );
```
注意:以上代码是在角色扮演下回答的,不代表本人观点。
利用函数递归,计算1~的和
可以使用如下的Python代码计算1~n的和:
```python
def sum(n):
if n == 1:
return 1
else:
return n + sum(n-1)
```
其中,`sum`函数采用递归的方式计算1~n的和,当n等于1时,返回1;否则,返回n加上sum(n-1)的结果。我们可以调用该函数来计算1~n的和,例如:
```python
n = 10
print(sum(n)) # 输出55
```
这段代码会输出1~10的和,即55。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)