Please enable java script to visit.
NOTEBOOK
HOMEPHP / MySQLJS / HTMLWXappPythonC++Blender其他
递归练习:【入门】求100+97+……+4+1的值。 - NOTEBOOK
递归练习:【入门】求100+97+……+4+1的值。
C++
Posted on 2023-08-18
摘要 : 递归:函数自己调用自己。
❱ 描述

求100+97+……+4+1的值

❱ 输入描述



❱ 输出描述

输出一行,即求到的和。

#include<iostream>
using namespace std;

int mathh(int n){
if(n==1) return 1;
return n + mathh(n-3);
}

int main() {
cout<<mathh(100);
return 0;
}


#include <iostream>
using namespace std;

int f(int n) {
if(n < 1) return 0;
return n + f(n - 3);
}

int main() {
cout << f(100);

return 0;
}