本程序思路是以一个人为起点,10天为一个周期,进行传染病的传染,当能够传染到其他人时,再以那个人为起点继续传染。依此递归下去。
public class Infection { static int N = 13; static int men = 1; public static void main(String[] args) { infection(N); System.out.println("第" + N + "天共有" + men + "个人被感染!"); System.out.println("************************************"); } public static void infection(int N){ int day =1; if(N <= 5){ return; }else if(N>5&&N<10){ for(day = 6;day <= N;day++){ men = men + 3; } }else{ for(day = 6;day <= 10;day++){ men = men + 3; infection(N-day+1); infection(N-day+1); infection(N-day+1); } men--; } } }