母牛的故事
#include <cstdio> int f(int n) { if(n == 0 || n == 1) return 1; if(n == 2) return 2; return f(n - 1) + f(n - 3); } int main() { int n; while(scanf("%d", &n) && n != 0) { printf("%d\n", f(n)); } return 0; }
#include <cstdio> int memo[60]; int f(int n) { memo[0] = memo[1] = 1; memo[2] = 2; for(int i = 3; i <= n; ++ i) { memo[i] = memo[i - 1] + memo[i - 3]; } return memo[n]; } int main() { int n; while(scanf("%d", &n) && n != 0) { printf("%d\n", f(n)); } return 0; }