C++

— 陳曉璇

  4 - 函式、遞迴

目錄 Index

函式

  F u n c t i o n

#include <iostream>
using namespace std;

int main()
{
    int a0,b0,a1,b1;
    cin >> a0 >> b0 >> a1 >> b1;
    //有四個數,兩兩一組比較大小
    if(a0>b0){
      cout>>a0>>endl;
    }
    else{
          cout>>b0>>endl;
    }
    
    if(a1>b1){
      cout>>a1>>endl;
    }
    else{
          cout>>b1>>endl;
    }
    
    return 0;
}
#include <iostream>
using namespace std;
//定義一個比大小的函式
int max(int i,int j)
{
    if(i>j){
          return i;
    }
    else{
          return j;
    }
}

int main()
{
    int a0,b0,a1,b1;
    cin >> a0 >> b0 >> a1 >> b1;
    cout << max(a0,b0) << endl;
    cout << max(a1,b1) << endl;
    return 0;
}

函式是什麼?

又稱為副程式,是一串程式區段的集合

#include <iostream>
using namespace std;

int main()
{
    int a0,b0,a1,b1;
    cin >> a0 >> b0 >> a1 >> b1;
    //有四個數,兩兩一組比較大小
    if(a0>b0){
      cout>>a0>>endl;
    }
    else{
          cout>>b0>>endl;
    }
    
    if(a1>b1){
      cout>>a1>>endl;
    }
    else{
          cout>>b1>>endl;
    }
    
    return 0;
}
#include <iostream>
using namespace std;
//定義一個比大小的函式
int max(int i,int j)
{
    if(i>j){
          return i;
    }
    else{
          return j;
    }
}

int main()
{
    int a0,b0,a1,b1;
    cin >> a0 >> b0 >> a1 >> b1;
    cout << max(a0,b0) << endl;
    cout << max(a1,b1) << endl;
    return 0;
}

函式的用處?

#include <iostream>
using namespace std;

int main()
{
    int a0,b0,a1,b1;
    cin >> a0 >> b0 >> a1 >> b1;
    //有四個數,兩兩一組比較大小
    if(a0>b0){
      cout>>a0>>endl;
    }
    else{
          cout>>b0>>endl;
    }
    
    if(a1>b1){
      cout>>a1>>endl;
    }
    else{
          cout>>b1>>endl;
    }
    
    return 0;
}
#include <iostream>
using namespace std;
//定義一個比大小的函式
int max(int i,int j)
{
    if(i>j){
          return i;
    }
    else{
          return j;
    }
}

int main()
{
    int a0,b0,a1,b1;
    cin >> a0 >> b0 >> a1 >> b1;
    cout << max(a0,b0) << endl;
    cout << max(a1,b1) << endl;
    return 0;
}
  • 避免編寫重複的程式碼
  • 方便閱讀程式
  • 方便除錯(Debug)

宣告

  • 回傳值的型別
  • 函式名稱
  • 參數列
#include <iostream>
using namespace std;

//宣告一個函式
int max( int , int );

int main()
{
    int a0,b0,a1,b1;
    cin >> a0 >> b0 >> a1 >> b1;
    cout << max(a0,b0) << endl;
    cout << max(a1,b1) << endl;
    return 0;
}

//定義一個比大小的函式
int max(int i,int j)
{
    if(i>j){
          return i;
    }
    else{
          return j;
    }
}

定義

  • 函式主體 {}

宣告+定義

  • 回傳值的型別
  • 函式名稱
  • 參數列
  • 函式主體 {}
#include <iostream>
using namespace std;
//定義一個比大小的函式
int max(int i,int j)
{
    if(i>j){
          return i;//回傳i
    }
    else{
          return j;//回傳j
    }
}

int main()
{
    int a0,b0,a1,b1;
    cin >> a0 >> b0 >> a1 >> b1;
    cout << max(a0,b0) << endl;
    cout << max(a1,b1) << endl;
    return 0;
}

宣告時

型別 名稱(參數1, 參數2...){
    函式主體;
    return 回傳值;
}
  • 放在主程式前
  • 每個參數的型別都要打出來

型別 void

  • 無回傳值(可省略 return)
//void使用範例:輸出兩數相加
void Plus(int a, int b){
    cout << a+b << endl;
}
  • <iostream>

C++內建函式庫

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int x,n,c;
    cin >> x >> n >> c;
    cout << pow(x,n) << endl;//x的n次方
    cout << sqrt(c) << endl;//c開根號
    return 0;
}

遞迴

R e c u r s i o n

  • 在函式裡呼叫自己
  • 把大問題拆解成小問題

遞迴

//範例:階乘的遞迴函式
int Factorial(int n){
    if(n==1){
    	return 1; //1!為1
    }
    else{
    	return n*Factorial(n-1); //n!為n*(n-1)!
    }
}

階乘

   ∵ n!=n(n-1)(n-2)(n-3)...2×1

 

 

(n-1)!

∴ n!=n×(n-1)!

遞迴

//範例:階乘的遞迴函式
int Factorial(int n){
    if(n==1){
        cout << n << ' ' << "return 1" << endl;
    	return 1; //1!為1
    }
    else{
        cout << n << ' ' << "return n*Factorial(n-1)" << endl;
    	return n*Factorial(n-1); //n!為n*(n-1)!
    }
}
//cout << Factorial(4);
4 return n*Factorial(n-1)
3 return n*Factorial(n-1)
2 return n*Factorial(n-1)
1 return 1
24
  • 呼叫自己的方式、條件
  • 定義終止條件

遞迴設計

//範例:階乘的遞迴函式
int Factorial(int n){
    if(n==1){
    	return 1; //終止條件
    }
    else{
    	return n*Factorial(n-1); //呼叫自己
    }
}

範例題目

  • 提示:

      F(0)=0 、 F(1)=1

      F(n)=F(n-1)+F(n-2)  n ≥ 2

F(0) F(1) F(2) F(3) F(4)
0 1 1 2 3
  • 提示:

      輾轉相除法

  • 重複動作:相除取餘數,用餘數再繼續相除取餘數
  • 結束條件:餘數為零
int gcd(int a, int b){
    if(b==0){
    	return a ;
    }
    else{
        int c = a%b;
    	return gcd(b,c) ;
        //也可以直接寫return gcd(b,a%b);
    }
}

Kahoot!

zsisc29th-4

By CHEN, SIAO SYUAN