数据类型

第二节:C++语言基础 - 图1

函数

第二节:C++语言基础 - 图2

递归函数

第二节:C++语言基础 - 图3

例如编写一求1+2+..+n的值,其中n<=20

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int sum(int n){
  4. if (n == 1){
  5. return 1;
  6. }
  7. return sum(n-1)+n;
  8. }
  9. int main(){
  10. int n;
  11. cin >> n;
  12. cout << sum(n);
  13. }