4 - 函式、遞迴
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;
}#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使用範例:輸出兩數相加
void Plus(int a, int b){
cout << a+b << endl;
}#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);
}
}