爬楼梯 斐波那契数列

 1 package com.gateWay;
 2 
 3 import java.io.InputStream;
 4 import java.util.Scanner;
 5 
 6 /**
 7  * @name: Step
 8  * @description:
 9  * @author: lin
10  * @version: v1.0
11  * @create: 2021-01-31 22:05
12  **/
13 public class Step {
14 
15     public static void main(String[] args) {
16         int m = 0;
17         Scanner scanner = new Scanner(System.in);
18         if (scanner.hasNextInt()){
19             m = scanner.nextInt();
20         }
21 
22         int a = 1;
23         int b = 2;
24         int c = 0;
25         if(m == 1){
26             System.out.println(a);
27             return;
28         }
29         if(m == 2){
30             System.out.println(b);
31             return;
32         }
33         System.out.printf(a + " "+ b);
34         while (m-- > 2){
35             c = a + b;
36             a = b;
37             b = c;
38             System.out.printf(" " + c);
39         }
40     }
41 }

 

posted @ 2021-01-31 22:23  SunLinux0523  阅读(32)  评论(0)    收藏  举报