一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶(n为正整数)总共有多少种跳法。
数据范围:
进阶:空间复杂度 , 时间复杂度
进阶:空间复杂度 , 时间复杂度
import java.io.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); int n = Integer.parseInt(s); int ans = 1 << (n-1); System.out.println(ans); } }
import java.util.Scanner; // 注意类名必须为 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 a = in.nextInt(); int b = in.nextInt(); System.out.println(a + b);*/ int num = in.nextInt(); int arr[] = new int[num + 1]; if(num == 1){ System.out.println(num); return; } //等比数列 //f(n) = f(n-1) + f(n-2) + ....+1 //因为f(n-1) = f(n-2) + f(n-3) + ....+1 //所以f(n) = f(n-1) + f(n-1)=2f(n-1) System.out.println((int)Math.pow(2,num - 1)); } } }