首页 > 试题广场 >

N阶楼梯上楼问题

[编程题]N阶楼梯上楼问题
  • 热度指数:21614 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
N阶楼梯上楼问题:一次可以走两阶或一阶,问有多少种上楼方式。(要求采用非递归)

输入描述:
输入包括一个整数N,(1<=N<90)。


输出描述:
可能有多组测试数据,对于每组数据,
输出当楼梯阶数是N时的上楼方式个数。
示例1

输入

4

输出

5
Java 解法
import java.util.Scanner;

public class Main {
    // 使用动态规划解法
    // 状态转移方程 dp[i]=dp[i-1]+dp[i-2]
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        // n+2 是为了防止数组下标越界,如输入1时
        int[] dp = new int[n+2];
        dp[1]= 1;
        dp[2] = 2;
        for (int i = 3; i <= n; i++) dp[i]=dp[i-1]+dp[i-2];
        System.out.println(dp[n]);

    }
}


发表于 2020-03-18 10:09:52 回复(0)
import java.util.Scanner;

public class Main {

@SuppressWarnings("resource")
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
for (int i = 1; i <=N; i++) {
System.out.println(getSum(i));
}
}
public static int getSum(int n) {
if (n==1) {
return 1;
}else if (n==2) {
return 2;
}else {
return getSum(n-1)+getSum(n-2);
}
}

}

发表于 2017-06-18 18:23:13 回复(0)
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
int n ;
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext())
{
n=scanner.nextInt();
while(n<1 ||n>90)
{
System.out.println("请输入大于1-90的整数");
n=scanner.nextInt();
}
System.out.println(fab(n));
}
}

public static long fab(int n)
{
if(n==1)
{
return 1;
}
if(n==2)
{
return 2;
}
else
{
long f1 = 1;
long f2 = 2;
for(int i = 3; i <= n; ++i){
f2 += f1;
f1 = f2 - f1;
}
return f2;
}

}
}

发表于 2017-05-30 16:16:50 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
int n=sc.nextInt();
long a=1,b=2,c=0;
if(n>=3)
{
for(int i=3;i<=n;i++)
{
c=a+b;
a=b;
b=c;
}
System.out.println(c);
}
else if(n==1)
{
System.out.println(a);
}
else if(n==2)
{
System.out.println(b);
}
        }
    }
}
//定义三个变量通过斐波那契数列即可完成此题
发表于 2017-03-09 10:48:38 回复(0)

本人使用的是组合数C(n,m)的方法来求的,虽然麻烦,但是很好理解。代码如下:

import java.math.BigInteger;

import java.util.Scanner;

public class Main {

        public static void main(String[] args) {

            Scanner scanner = new Scanner(System.in);

            while (scanner.hasNext()) {

                int n = scanner.nextInt();

                BigInteger count = new BigInteger("0");

                int x;

                for (int y = 0; y <= n / 2; y++) {            //y: 两阶      x:一阶

                    x = n - 2 * y;

                if (y == 0 || x == 0) {

                    count = count.add(BigInteger.valueOf(1));

                } else {

                    count = count.add(zuhe(y, x + y));             //总步数中两阶的取法

                }

                }

        System.out.println(count);
        }

        scanner.close();

    }

    public static BigInteger jc(int n) {             //求n的阶乘

        BigInteger sum = new BigInteger("0");

        if (n == 1) {

            sum = BigInteger.valueOf(1);

        } else {

            sum = BigInteger.valueOf(n).multiply(jc(n - 1));

        }

        return sum;

    }

    public static BigInteger zuhe(int n, int m) {   //求组合数C(n,m)

        return (jc(m).divide(jc(n).multiply(jc(m-n))));

    }

}

编辑于 2017-02-25 12:27:14 回复(0)
package com.mytest.mymain;

import java.io.*;

public class Main{
	public static void main(String[] args) throws Exception{
		java.util.Scanner sc=new java.util.Scanner(System.in);//001
		String str;
		while((str=sc.nextLine())!=null){  //002
			int n=Integer.parseInt(str);
			long[] result=new long[n+2];
			result[1]=1;
            result[2]=2;
            if(n==1 || n==2 ){
                System.out.println(result[n]);
            }else{
			for(int i=3;i<=n;i++){
				result[i]=result[i-1]+result[i-2];
			}	
			System.out.println(result[n]);
            }
		}
      /* BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       String str;
		while((str=br.readLine())!=null){
			int n=Integer.parseInt(str);
			long[] result=new long[n+2];
			result[1]=1;
           result[2]=2;
           if(n==1 || n==2 ){
               System.out.println(result[n]);
           }else{
			for(int i=3;i<=n;i++){
				result[i]=result[i-1]+result[i-2];
			}	
			System.out.println(result[n]);
           }
		}*/
			
	}
}
备注:为何用Scanner 自己线下可以,但是这个上面却不通过,看来对输入输出流还不够了解,需要进一步学习,欢迎精通的人,给出关于用Scanner不通过的原因。代码就两处不同,001和002处。
		
发表于 2017-01-10 17:21:55 回复(1)
import java.util.Scanner;

public class N_stair {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext())
{
String input=scanner.nextLine();
long num=Long.parseLong(input);
if(num==1||num==2)
{
System.out.println(String.valueOf(num));
}
else if (num>2) {
long[] buffer=new long[(int) num];
buffer[0]=1;
buffer[1]=2;
for(int i=2;i<num;i++)
{
buffer[i]=buffer[i-1]+buffer[i-2];
}
System.out.println(buffer[(int) (num-1)]);
}
else {
System.out.println("错误");
}

}
}
}

发表于 2017-01-06 20:24:41 回复(0)