By GokiSoft.com|
19:58 01/02/2023|
Java Basic
[Source Code] In chuỗi Fibonaci - C2206L
#Fibonacci.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson02;
import java.util.Scanner;
/**
*
* @author student
*/
public class Fibonacci {
public static void main(String[] args) {
// testcase01();
testcase02();
}
static void testcase02() {
int fn = 1, max, i = 1;
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so max = ");
max = Integer.parseInt(scan.nextLine());
System.out.println("Day fibonacci\n");
while (fn <= max) {
System.out.print(", " + fn);
fn = f(i++);
}
}
static int f(int n) {
if(n == 0 || n == 1) return 1;
return f(n - 1) + f(n - 2);
}
static void testcase01() {
int f0 = 1, f1 = 1;
int fn, max;
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so max = ");
max = Integer.parseInt(scan.nextLine());
System.out.print("\n" + f0 + ", " + f1);
fn = f0 + f1;
while(fn <= max) {
System.out.print(", " + fn);
f0 = f1;
f1 = fn;
fn = f0 + f1;
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)