在一行上输入一个整数
代表查询的月份。
在一行上输出一个整数,代表第
个月的兔子总数。
3
2
第一个月时,只有初始兔子(记为
),此时兔子总数为
。
第二个月时,依旧只有
,此时兔子总数为
。
第三个月时,
开始生兔子(记生出来的兔子为
),此时兔子总数为
。
第四个月时,
再生一只兔子,此时兔子总数为
。
第五个月时,
再生一只兔子,与此同时
也开始生兔子,此时兔子总数为
。
5
5
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int a = in.nextInt(); int[] k=new int[2]; k[0]=1; for(int i=1;i<a;i++){ int temp=k[1]; k[1]=k[0]+k[1]; k[0]=temp; } System.out.println(k[0]+k[1]); } }
基本理解:
递推关系:
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if(n == 1 || n == 2){ System.out.println(1);; return; } int prev1 = 1; int prev2 = 1; int current = 0; for (int i = 3; i <= n; i++) { current = prev1 + prev2; prev2 = prev1; prev1 = current; } System.out.println(current); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 System.out.println(f(in.nextInt())); } public static int f (int input) { if (input<=0) { return 0; } if (input==1 || input==2) { return 1; } return f(input-1)+f(input-2); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int month = in.nextInt(); int rbbits = 1; System.out.println(birth(rbbits,month)); } public static int birth(int rabbits,int month){ for(int i = 0; i <= month ;i++){ if(i>2){ rabbits += birth(1,month-i+1); } } return rabbits; } }
import java.util.HashMap; import java.util.Map; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int len = in.nextInt(); Map<Integer, Integer> map = new HashMap(); map.put(1, 1); map.put(2, 0); map.put(3, 0); for (int i = 1 ; i < len; i++) { int old2 = map.get(2); map.put(2, map.get(1)); map.put(1, map.get(3) + old2); map.put(3, map.get(3) + old2); } int re = 0; for (int key : map.keySet()) { re += map.get(key); } System.out.print(re); } }
import java.util.Scanner; import java.io.*; public class Main { public static void main(String[] args) throws IOException{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(reader.readLine()); int smallrabbit = 1; int middlerabbit = 0; int bigrabbit = 0; for(int i = 1;i < n;i++){ bigrabbit += middlerabbit; middlerabbit = smallrabbit; smallrabbit = bigrabbit; } System.out.print(smallrabbit + middlerabbit + bigrabbit); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); // Attention difference between hasNext and hasNextLine //一看就知道是 斐波那契数列 // 1 2 3 4 5 6 月份 // 1 1 2 3 5 8 数量 // A(n)=A(n-1) + A (n-2) //矩阵表达 ; // |A(n) | == | 1 1 | ** | A(n-1) | ==| 1 1| ^^ (n-1) ** | A(1) = 1| // |A(n-1) | == | 1 0 | ** | A(n-2) | ==| 1 0| ** | A(0) = 0| int[][] M = {{1, 1}, {1, 0}}; int[][] res = {{1, 0}, {0, 1}}; int t = n - 1; while (t > 0) { if ((t & 1) == 1) { res = matrixMul(res, M); } M = matrixMul(M, M); t >>>= 1; } System.out.println(res[0][0]); } private static int[][] matrixMul(int[][] m1, int[][] m2) { int[][] res = new int[2][2]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { res[i][j] = m1[i][0] * m2[0][j] + m1[i][1] * m2[1][j]; } } return res; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); System.out.println(dp(n)); } } public static int dp(int n) { int num[] = new int[n]; num[0] = 1; num[1] = 1; for (int i = 2; i < n; i++) { num[i] = num[i - 1] + num[i - 2]; } return num[n-1]; } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int num = in.nextInt();//5 //1 2 3 4 5 6 //1 1 2 3 5 8 int res = 1; List<Integer> arr= new ArrayList();//记录每个月的兔子数 arr.add(1);//第一天 if (num > 2) { for (int i = 1; i < num - 1; i++) { arr.add(res); res = arr.get(i-1) + res; } } else { System.out.println(1); } System.out.println(res); } } }
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int months = in.nextInt();
//定义一个容器,记录1,2和 3月以上的兔子数量
int[] r = new int[3];
//初始时候只有一只一月的兔子
r[0]=1;
while(months>0){ int orgOne = r[0]; int orgTwo = r[1]; int orgThr = r[2]; if(orgOne>0){ //如果有一月份的数据 ,全量递增到二月份 r[0]-=orgOne; r[1]+=orgOne; } //如果二月份之前有数据 ,全量递增到三月份 if(orgTwo>0){ r[1]-=orgTwo; r[2]+=orgTwo; } //如果三月份有数据,全量新增到一月 if(orgThr>0){ r[0]+=r[2]; } months--; } System.out.println(r[0]+r[1]+r[2]); } }
}
import java.util.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); int n = Integer.parseInt(line); /** * 1 2 3 4 5 6 7 * 1 1 2 3 5 */ System.out.println(rabbit(n)); } /** * 斐波那契数列,递归思想 * * @param n * @return */ private static int rabbit(int n) { if (n <= 2) { return 1; } else { return rabbit(n - 1) + rabbit(n - 2); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int month=in.nextInt(); int arr[]=new int[]{1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269}; System.out.print(arr[month-1]); } }