2016년 7월 16일 토요일

c언어 자료구조론 - 피보나치수열2 (구조체사용)

2009-04-26

high-ordered recursion으로 구성되어 시간복잡도가 컸던 함수를

이전 게시글에 있는 피보나치수열과 마찬가지로 linear recursion으로 구성한것이에용.

이전과는 다르게 구조체를 사용하여 표현하였습니다.


#include <stdio.h>
typedef struct{
 int n0;
 int n1;
}Two;   // 구조체선언할때도 ;를 붙이는구낭...

Two fib(int n){

 Two bef;
 Two pre;

 if(n<2){
  bef.n0=0;
  bef.n1=1;
  return bef;}   // }뒤에 ;있어도 같은결과를 출력
 bef=fib(n-1);
 pre.n0=bef.n1;
 pre.n1=bef.n0+bef.n1;
 return pre;}

void main(){
 int n;
 Two tem;
 printf("피보나치수열의 n번째항을 구해드립니당.\nn을 입력하세요.\n");
 scanf("%d",&n);
 tem=fib(n);
 printf("피보나치수열의 %d번째 항은 %d입나다.^3^\n",n,tem.n1);
}

댓글 없음:

댓글 쓰기