d ata s tructures

53
DONGEUI UNIVERSITY Data structures 동동동동동 동동동동동동동동 동동동 동동 02.1: recursive functions

Upload: chogan

Post on 18-Jan-2016

36 views

Category:

Documents


2 download

DESCRIPTION

D ata s tructures. 02.1: r ecursive f unctions. 동의대학교 멀티미디어공학과 이광의 교수. T oday’s t opic. 재귀함수의 예와 동작방법 계승 함수와 그 동작방법 피보나치 함수와 그 동작방법 최소공배수 함수 mathematical induction 수학적 귀납법 계승 함수의 증명 피보나치 함수의 증명 최소공배수 함수의 증명 재귀함수의 활용 징검다리 건너기 당근 캐기 거북이 옮기기. e xamples o f r ecursive f unction. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: D ata  s tructures

DONGEUIUNIVERSITY

Data structures

동의대학교 멀티미디어공학과 이광의 교수

02.1: recursive functions

Page 2: D ata  s tructures

[email protected]

Today’s topic 재귀함수의 예와 동작방법

► 계승 함수와 그 동작방법► 피보나치 함수와 그 동작방법► 최소공배수 함수

mathematical induction 수학적 귀납법► 계승 함수의 증명► 피보나치 함수의 증명► 최소공배수 함수의 증명

재귀함수의 활용► 징검다리 건너기► 당근 캐기► 거북이 옮기기

Dept. of Multimedia Engineering, DongEui Univ.

Page 3: D ata  s tructures

[email protected]

examples of recursive function 재귀 , 재귀적 구조 그리고 재귀함수란 무엇인가 ?

► recursion 재귀 : ?► recursive structure 재귀적 구조 : 자기 내부에 자기자신과 동일한 구조를

포함하는 구조► recursive function 재귀함수 : 자기자신을 호출하는 함수

재귀함수의 예► factorial function 계승함수► fibonacci sequence 피보나치 수열► greatest common divisor 최대공약수

Dept. of Multimedia Engineering, DongEui Univ.

Page 4: D ata  s tructures

[email protected]

examples of recursive function n! factorial 계승 함수의 정의 [wikipedia.org]

► 계승함수는 다음과 같이 정의된다 .

• 1! = 1;• 5! = 1*2*3*4*5 = 120;• 6! = 1*2*3*4*5*6 = 720;

► 또는 재귀적으로 다음과 같이 정의된다 .

• 0! = 1;• 1! = 1(0!) = 1*1 = 1;• 2! = 2(1!) = 2*1 = 2;• 5! = 5(4!) = 5*4! = 5*4*3! = 5*4*3*2! = 5*4*3*2*1! = 5*4*3*2*1 = 120;

Dept. of Multimedia Engineering, DongEui Univ.

Nnknn

k

1

!

Nnnn

n

0n if

0n if

)!1(

1!

Page 5: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 구현

int factorial (int n) { // n>0 int rFac (int n) { // n>0;

int f = 1; if (n==1) return 1; // if (n==0) return 1;

for (int i=1; i<=n; i++) { else return n*rFac (n-1);

f = f*i; }

}

return f; void main ( ) {

} cout << rFac(3); // 6 을 출력}

Dept. of Multimedia Engineering, DongEui Univ.

Nnknn

k

1

! Nnnn

n

1n if

1n if

)!1(

1!

Page 6: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 구현

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

Dept. of Multimedia Engineering, DongEui Univ.

Page 7: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) {◀ cout << rFac(3);

}

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

Page 8: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) {

cout << rFac(3);◀ if (3==1) return 1;

return 3*rFac(2);

} }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

Page 9: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) {◀ cout << rFac(3);◀ if (3==1) return 1;

return 3*rFac(2);

} }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

Page 10: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) {

cout << rFac(3);◀ if (3==1) return 1;◀ return 3*rFac(2);

} }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

Page 11: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) {

cout << rFac(3);◀ if (3==1) return 1;

return 3*rFac(2);◀} }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

Page 12: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) {

cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1;

return 3*rFac(2);◀ return 2*rFac(1);

} } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

Page 13: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) {◀ cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1;

return 3*rFac(2);◀ return 2*rFac(1);

} } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

Page 14: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) {

cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1;◀ return 3*rFac(2);◀ return 2*rFac(1);

} } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

Page 15: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) { int rFac(1) {

cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1; if (1==1) re-turn 1;

return 3*rFac(2);◀ return 2*rFac(1);◀ return 1*rFac(0);

} } } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

Page 16: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) { int rFac(1) {◀ cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1; if (1==1) re-

turn 1;

return 3*rFac(2);◀ return 2*rFac(1);◀ return 1*rFac(0);

} } } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

Page 17: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) { int rFac(1) {

cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1; if (1==1) return 1;◀

return 3*rFac(2);◀ return 2*rFac(1);◀ return 1*rFac(0);

} } } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

Page 18: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) { int rFac(1) {

cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1; if (1==1) return 1;◀

return 3*rFac(2);◀ return 2*rFac(1);◀ return 1*rFac(0);

} } } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

1

Page 19: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) {

cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1;

return 3*rFac(2);◀ return 2*rFac(1);◀} } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

1

Page 20: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) {

cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1;

return 3*rFac(2);◀ return 2*rFac(1);◀} } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

12

Page 21: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) {

cout << rFac(3);◀ if (3==1) return 1;

return 3*rFac(2);◀} }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

2

Page 22: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) {

cout << rFac(3);◀ if (3==1) return 1;

return 3*rFac(2);◀} }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

26

Page 23: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) {

cout << rFac(3);◀

}

6 이 출력됨

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

6

Page 24: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) {

cout << rFac(3);

}◀6 이 출력됨프로그램 종료됨

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

Page 25: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) {◀ cout << rFac(3);

}

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

main ()

Page 26: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) {

cout << rFac(3);◀ if (3==1) return 1;

return 3*rFac(2);

} }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

main ()

rFac()

Page 27: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) {◀ cout << rFac(3);◀ if (3==1) return 1;

return 3*rFac(2);

} }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

main ()

rFac()

N=3

Page 28: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) {

cout << rFac(3);◀ if (3==1) return 1;◀ return 3*rFac(2);

} }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

main ()

rFac()

N=3

Page 29: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) {

cout << rFac(3);◀ if (3==1) return 1;

return 3*rFac(2);◀} }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

main ()

rFac()

N=3

Page 30: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) {

cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1;

return 3*rFac(2);◀ return 2*rFac(1);

} } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

main ()

rFac()

rFac ()

N=3

Page 31: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) {◀ cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1;

return 3*rFac(2);◀ return 2*rFac(1);

} } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

main ()

rFac()

rFac ()

N=3

N=2

Page 32: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) {

cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1;◀ return 3*rFac(2);◀ return 2*rFac(1);

} } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

main ()

rFac()

rFac ()

N=3

N=2

Page 33: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) { int rFac(1) {

cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1; if (1==1) re-turn 1;

return 3*rFac(2);◀ return 2*rFac(1);◀ return 1*rFac(0);

} } } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

main ()

rFac()

rFac ()

N=3

N=2

rFac ()

Page 34: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) { int rFac(1) {◀ cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1; if (1==1) re-

turn 1;

return 3*rFac(2);◀ return 2*rFac(1);◀ return 1*rFac(0);

} } } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

main ()

rFac()

rFac ()

rFac ()

N=3

N=2

N=1

Page 35: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) { int rFac(1) {

cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1; if (1==1) return 1;◀

return 3*rFac(2);◀ return 2*rFac(1);◀ return 1*rFac(0);

} } } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

main ()

rFac()

rFac ()

rFac ()

N=3

N=2

N=1

Page 36: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) { int rFac(1) {

cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1; if (1==1) return 1;◀

return 3*rFac(2);◀ return 2*rFac(1);◀ return 1*rFac(0);

} } } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

1

main ()

rFac()

rFac ()

rFac ()

N=3

N=2

N=11

Page 37: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) {

cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1;

return 3*rFac(2);◀ return 2*rFac(1);◀} } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

1

main ()

rFac()

rFac ()

N=3

N=2

1

Page 38: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) { int rFac(2) {

cout << rFac(3);◀ if (3==1) return 1; if (2==1) return 1;

return 3*rFac(2);◀ return 2*rFac(1);◀} } }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

12

main ()

rFac()

rFac ()

N=3

N=22

1

Page 39: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) {

cout << rFac(3);◀ if (3==1) return 1;

return 3*rFac(2);◀} }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

2

main ()

rFac()

N=3

2

Page 40: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) { int rFac(3) {

cout << rFac(3);◀ if (3==1) return 1;

return 3*rFac(2);◀} }

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

26

main ()

rFac()

N=36

2

Page 41: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) {

cout << rFac(3);◀

}

// 6 이 출력됨

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

6

main ()

6

Page 42: D ata  s tructures

[email protected]

examples of recursive function 계승함수의 동작

int rFac (int n) { // n>0;

if (n==1) return 1; // if (n==0) return 1;

else return n*rFac (n-1);

}

void main ( ) {

cout << rFac(3); // 6 을 출력}

void main ( ) {

cout << rFac(3);

}◀// 6 이 출력됨// 프로그램 종료됨

Dept. of Multimedia Engineering, DongEui Univ.

실행과정

main ()

Page 43: D ata  s tructures

[email protected]

examples of recursive function Fibonacci number 피보나치수의 정의

► In mathematics, the Fibonacci numbers are the following sequence of numbers:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …• By definition, the first two Fibonacci numbers are 0 and 1,

and each remaining number is the sum of the previous two.• Some sources omit the initial 0,

instead beginning the sequence with two 1s.

► In mathematical terms, the sequence Fn of Fibonacci numbers is de-fined by the recurrence relation

with seed values:

Dept. of Multimedia Engineering, DongEui Univ.

21 nnn FFF

1Fand0 10 F

Page 44: D ata  s tructures

[email protected]

examples of recursive function 피보나치 수를 계산하는 피보나치 함수의 구현

int fibonaaci (int n) { int rF(int n) { // assume that n>=0

int fibo[n+1]; if (n<2) return n; // if (n==0||n==1) re-turn n;

fibo[0] = 0; else return rF(n-1)+rF(n-2);

fibo[1] = 1; }

for (int i=2; i<=n; i++) {

fibo[i] = fibo[i-1]+fibo[i-2]; void main ( ) {

} cout << rF(4);

return fibo[n]; }

}

Dept. of Multimedia Engineering, DongEui Univ.

21 nnn FFF

1and0 10 FF 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

Page 45: D ata  s tructures

[email protected]

examples of recursive function 피보나치 함수의 동작

int rF(int n) { // assume that n>=0

if (n<2) return n;

return rF(n-1)+rF(n-2);

}

void main ( ) {

cout << rF(0);

}

Dept. of Multimedia Engineering, DongEui Univ.

main ()

rF(0)

0 0

Page 46: D ata  s tructures

[email protected]

examples of recursive function 피보나치 함수의 동작

int rF(int n) { // assume that n>=0

if (n<2) return n;

return rF(n-1)+rF(n-2);

}

void main ( ) {

cout << rF(1);

}

Dept. of Multimedia Engineering, DongEui Univ.

main ()

rF(1)

1 1

Page 47: D ata  s tructures

[email protected]

examples of recursive function 피보나치 함수의 동작

int rF(int n) { // assume that n>=0

if (n<2) return n;

return rF(n-1)+rF(n-2);

}

void main ( ) {

cout << rF(2);

}

Dept. of Multimedia Engineering, DongEui Univ.

main ()

rF(2)

rF(1)

rF(0)

1 0

1 0

2 1

Page 48: D ata  s tructures

[email protected]

examples of recursive function 피보나치 함수의 동작

int rF(int n) { // assume that n>=0

if (n<2) return n;

return rF(n-1)+rF(n-2);

}

void main ( ) {

cout << rF(3);

}

Dept. of Multimedia Engineering, DongEui Univ.

main ()

rF(3)

rF(2)

rF(1)

rF(0)

rF(1)

2 1

1 0

1 0

1 1

3 2

Page 49: D ata  s tructures

[email protected]

examples of recursive function 피보나치 함수의 동작

int rF(int n) { // assume that n>=0

if (n<2) return n;

return rF(n-1)+rF(n-2);

}

void main ( ) {

cout << rF(4);

}

Dept. of Multimedia Engineering, DongEui Univ.

main ()

rF(4)

rF(3)

rF(2)

rF(1)

rF(2)

rF(0)

rF(1)

rF(0)

rF(1)

3 2

2 1 1 0

1 0

1 0

1 1

2 1

4 3

1 0

Page 50: D ata  s tructures

[email protected]

examples of recursive function 피보나치 함수의 동작

int rF(int n) { // assume that n>=0

if (n<2) return n;

return rF(n-1)+rF(n-2);

}

void main ( ) {

cout << rF(4);

}

Dept. of Multimedia Engineering, DongEui Univ.

main ()

rF(6)

rF(5)

rF(4)

rF(3)

rF(4)

rF(2)

rF(3)

5 4

4 3 3 2

3 2

5 3

6 8

2 1

Page 51: D ata  s tructures

[email protected]

examples of recursive function greatest common divisor 최대공약수의 정의 [wiki]

► In mathematics, the greatest common divisor (GCD), also known as the greatest common factor (GCF) or highest common factor (HCF), of two or more non-zero integers, is the largest positive integer that divides the numbers without a remainder.

GCD 를 구하는 방법 : Euclidian method 유클리드 호제법► a 가 b 보다 큰 경우 a 와 b 의 최대공약수는 다음과 같다 .

► GCD 최대공약수의 계산 예• GCD(3,0) = 3• GCD(9,3) = GCD(3,0) = 3• GCD(24, 10) = GCD(10, 4) = GCD(4,2) = GCD(2,0) = 2• GCD(3,9) = GCD(9, 3) = GCD(3,0) = 3

Dept. of Multimedia Engineering, DongEui Univ.

otherwise

bif

babGCD

abaGCD

)0(

)mod,(),(

Page 52: D ata  s tructures

[email protected]

examples of recursive function 최대공약수를 구하는 GCD 함수의 구현

► a 가 b 보다 큰 경우 a 와 b 의 최대공약수는 다음과 같다 .

int gcd (int a, int b) { int gcd (int a, int b) {

while (b!=0) { if (b==0) return a;

int tmp = a; return gcd(b, a%b);

a = b }

b = tmp%b;

} void main ( ) {

return a; cout << gcd(3,9);

} }

위의 재귀함수 GCD() 가 정말 잘 동작하는가 ???

Dept. of Multimedia Engineering, DongEui Univ.

otherwise

bif

babGCD

abaGCD

)0(

)mod,(),(

Page 53: D ata  s tructures

[email protected]

summaries 재귀함수의 예와 동작방법

► 계승 함수와 그 동작방법► 피보나치 함수와 그 동작방법► 최소공배수 함수

Dept. of Multimedia Engineering, DongEui Univ.

You are the one

Who makes that happen!!!