짝수 피보나치 수의 합
//문제
피보나치 수열: 1, 1, 2, 3, 5, 8, 13, ...
첫번째 + 두번째 = 세번째
두번째 + 세번째 = 네번째
세번째 + 네번째 = 다섯번째
...
피보나치 배열은 0과 1로 시작하며, 다음 피보나치 수는 바로 앞의 두 피보나치 수의 합이 된다.
정수 n이 주어지면, n보다 작은 모든 짝수 피보나치 수의 합을 구하여라.
//예제
input: 12
output: 10 // 0, 1, 2, 3, 5, 8 중 짝수인 2 + 8 = 10.
//
// main.c
// program
//
// Created by Eon on 7/26/18.
// Copyright © 2018 Eonseok Yim. All rights reserved.
//
#include <stdio.h>
int fibo (int n) {
if ((n==1) || (n==2))
return 1;
else
return fibo(n-2) + fibo(n-1);
}
int main(int argc, const char * argv[]) {
printf("input: ");
int input = 0 , result = 0, i = 1, fibo_num = 0;
scanf("%d", &input);
while(fibo_num <= input) {
fibo_num = fibo(i);
if (fibo_num%2 == 0)
result += fibo_num;
i++;
}
printf("output: %d\n", result);
return 0;
}
//실행
Eons-MacBook-Pro:Debug eon$ ls
program
Eons-MacBook-Pro:Debug eon$ ./program
input: 12
output: 10
Eons-MacBook-Pro:Debug eon$ ./program
input: 100
output: 188
Eons-MacBook-Pro:Debug eon$
더 짧게 짤수 있을것같은데... fibo 함수의 리턴값을 짝수만 리턴되도록하면...?? 더욱 간결해질것같다..