题解 | #数字颠倒#

统计每个月兔子的总数

http://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395

import java.util.Scanner;

public class Main {
public static void main(String[] args) {

    //不死神兔,即是斐波那契数列

// 方法一:递归
// Scanner sc = new Scanner(System.in);
// while (sc.hasNext()){
// int month = sc.nextInt();
//
// HJ37 hj37 = new HJ37();
// int qu = hj37.quantity(month);
// System.out.println(qu);
// }
// }
//
// public int quantity(int n){
//// int sum = 1;
// if(n == 1 || n == 2) {
// return 1;
// }
// return quantity(n-1) + quantity(n-2);
// 方法二:交替前进
Scanner sc = new Scanner(System.in);
while (sc.hasNext()){
int in = sc.nextInt();
int a = 1,b = 1, sum;
for(int i=1; i<in; i++){
sum = a+b;
a = b;
b = sum;
}
System.out.println(a);
}
}
}

全部评论

相关推荐

每晚夜里独自颤抖:要求太多的没必要理
点赞 评论 收藏
分享
06-26 17:24
已编辑
宁波大学 golang
迷失西雅图:别给,纯kpi,别问我为什么知道
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务