public static int cowNumber(int n) { if(n<1) return 0; if(n==1||n==2||n==3) return n; int pre1=3; int pre2=2; int pre3=1; int res=0; for(int i=4;i<=n;i++) { res=pre3+pre1; pre3=pre2; pre2=pre1; pre1=res; } return res; }