标签:
母牛每年的年初生一头小母牛
小母牛第四年年初生一头小母牛
第一年母牛数为1
推理第n年有多少母牛?
#include <stdio.h> /// n = new o = old /// n(i) = o(i-1) /// o(i) = o(i-1) + n(i-3) /// sum(i) = o(i) + n(i) + mid /// so: i miss the mid /// ex:fib int f(int i) { int a[55] = {0,1,2,3,4}; if(i<4) return a[i]; else return f(i-1) + f(i-3); } int main(void) { int n; while(scanf("%d",&n)) { printf("%d\n",f(n)); } return 0; }
参考答案
#include <ctype.h> #include <stdio.h> int main(void) { int n, i; int fab[55] = {1, 2, 3, 4, 6}; for (i = 5 ; i < 55 ; i++) fab[i] = fab[i - 1] + fab[i - 3]; while (scanf("%d", &n), n) { printf("%d\n", fab[n - 1]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/ailx10/p/5334249.html