zoukankan      html  css  js  c++  java
  • 某金融公司一个编程题

    1,1,2,3,5,8,13,21......请设计一个程序,第n项的值?

    public class MoneyTest {
        public static void main(String[] args) throws IOException {
            InputStream in = System.in;
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            System.out.println("请输入第几项:");
            String s = br.readLine();
            int res = method(Integer.valueOf(s));
            System.out.println(res);
        }
    
        private static int method(Integer n) {
            if(n == 1 || n == 2){
                return 1;
            }
            int i = 1, j = 1, m = 0;
            boolean b = true; //定义此b是为了 交替给i和j赋值
            while(n > 2){
                m = i + j;
                if(b){
                    b = false;
                    i = m;
                }else{
                    b = true;
                    j = m;
                }
                n --;
            }
            return m;
        }
    }
    

      

  • 相关阅读:
    团队冲刺03
    梦断代码 阅读笔记02
    团队冲刺02
    团队冲刺01
    周总结
    团队工作任务
    阅读笔记3
    梦断代码阅读笔记01
    周总结
    NABCD项目分析
  • 原文地址:https://www.cnblogs.com/liyong888/p/9270870.html
Copyright © 2011-2022 走看看