zoukankan      html  css  js  c++  java
  • 有一分数序列:2/1, 3/2, 5/3, 8/5, 13/8, 21/13....求出这个数列的第M到N项之和(M>2,N>2,N>M)

    package bianchengti;
    /*
     * 有一分数序列:2/1, 3/2, 5/3, 8/5, 13/8, 21/13....
     * 求出这个数列的第M到N项之和(M>2,N>2,N>M)
     */
    public class getSumByMN {
        //返回项:1, 2, 3, 5, 8, 13, 21....
        public static int getItem(int n) {
            if(n<0)
                return 0;
            int []array = {0,1,2};
            if(n<3)
                return array[n];
            return getItem(n-1)+getItem(n-2);
        }
        
        //返回项:2/1, 3/2, 5/3, 8/5, 13/8, 21/13....
        public static double getNumber(int n) {
            return (double)getItem(n+1) / getItem(n);
        }
        
        //返回第M到N项之和
        public static double getSum(int m,int n) {
            double sum = 0.0;
            for(int i = m; i <= n; i++){
                sum += getNumber(i);  
            }
            return sum;
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println(getSum(3, 4));    //3.2666666666666666
        }
    
    }
  • 相关阅读:
    bean的作用域和生命周期
    装配Bean的三种方式
    BeanFactory和ApplicationContext对比
    创建 Spring容器的三种方式
    约束
    三大范式
    事务
    EdgeRank
    TCP三次握手四次挥手
    Linux权限解释
  • 原文地址:https://www.cnblogs.com/liuzhenping/p/7592773.html
Copyright © 2011-2022 走看看