zoukankan      html  css  js  c++  java
  • 阶乘、斐波那契数列(java版)

     1 public class Demo
     2 {
     3     public static void main(String[] arg){
     4 
     5         System.out.println("10的阶乘是:"+fact(10));
     6         
     7         System.out.println("");
     8 
     9         System.out.print("斐波那契数列:");
    10 
    11         int count=12;
    12         for(int i=0;i<count;i++){
    13             if(i<count-1){
    14                 System.out.print(fib(i)+",");
    15             }else{
    16                 System.out.print(fib(i));
    17             }
    18         }
    19     }
    20 
    21     //阶乘
    22     private static int fact(int num){
    23         if(num==0 || num==1){
    24             return 1;
    25         }
    26         return num*fact(num-1);
    27     }
    28     
    29     //斐波那契数列
    30     private static int fib(int num){
    31         if(num<2)return 1;
    32         return fib(num-1)+fib(num-2);
    33     }
    34 }

    运行结果:

  • 相关阅读:
    自然拼读
    windws蓝屏解决方案
    chrome
    ubuntu安装英伟达驱动
    ubuntu基础
    kvm(未完成2021-04-26)
    istio
    OpenSSH
    su 与 su -关系
    read命令/ declare/set
  • 原文地址:https://www.cnblogs.com/comrd/p/3245269.html
Copyright © 2011-2022 走看看