羊が羊を産む

1739 ワード


package com.demo.sheep;

import java.math.BigInteger;

public class BirthSheep {

	public static void main(String[] args) {
		// Farm f = new Farm();
		// Sheep sheep = new Sheep(f);
		// f.addSheep(sheep);
		//
		// // low
		// for (long i = 0; i < 100; i++) {
		// f.getOlder();
		// System.out.prlongln((i+1)+" years later, the farm has "+
		// f.getQty()+" sheeps");
		// }

		BigInteger[] year = new BigInteger[300];
		year[0] = BigInteger.valueOf(1);

		for (int i = 1; i < year.length; i++) {
			year[i] = getFirst(year, i).add(getTwice(year, i));
			killSheep(year, i);
			System.out.println(i + " years later, the farm has "
					+ countSheep(year) + " sheeps");
		}

	}
	public static void killSheep(BigInteger[] year, int y) {
		if (y >= 5) {
			year[y - 5] = BigInteger.valueOf(0);
		}
	}
	public static BigInteger countSheep(BigInteger[] year) {
		BigInteger sum = BigInteger.valueOf(0);
		for (int i = 0; i < year.length; i++) {
			if(year[i]!=null){
				sum=sum.add(year[i]);
			}
		}
		return sum;
	}

	public static BigInteger getFirst(BigInteger[] year, int y) {

		if (y - 2 >= 0) {
			return year[y - 2];
		} else {
			return BigInteger.valueOf(0);
		}

	}
	public static BigInteger getTwice(BigInteger[] year, int y) {
		if (y - 4 >= 0) {
			return year[y - 4];
		} else {
			return BigInteger.valueOf(0);
		}

	}
}