import java.util.Scanner;
public class PaLouTi {
/**
* 爬楼 每次只能一层 两层 爬 有多少种方式爬完n层
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("爬多少层台阶?:");
int num = sc.nextInt();
if (num == 1) {
System.out.println("有一种方式");
} else if (num == 2) {
System.out.println("有两种方式");
} else {
int x = 1;
int y = 2;
int z = 0;
for (int i = 3; i <= num; i++) {
z = x + y;
y = z;// z赋值给y
x = y; // y赋值给x
// 为了下一把计算的准备
}
System.out.println("有" + z + "种可能结果");
}
sc.close();
}
}
3-1 经典例题:爬楼梯, 每次只能一层 两层 爬 有多少种方式爬完n层?
最新推荐文章于 2024-07-12 15:51:39 发布