By GokiSoft.com| 20:26 30/01/2023|
Java Basic

In chuỗi Fibonaci

Viết chương trình hiển thị dãy Fibonaci biết

F(0) = 1

F(1) = 1

F(n) = F(n-1) + F(n-2)

Nhập vào số max. Yêu cầu in ra tất cả các số Fibonaci có số lớn nhất là max bằng các giải thuật sau

1. Dùng đệ quy

2. Dùng đệ quy có nhớ

3. Không dùng đệ quy

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Nguyen Tri Duc [java1_online]
Nguyen Tri Duc

2023-04-15 16:28:49

Không dùng đệ quy.


import java.util.Scanner;

public class InSoFibo {
	public static void main(String[] args) {
		 Scanner sc = new Scanner(System.in);
		 System.out.println("Nhap max: ");
		 int max = sc.nextInt();
		 int f1 = 0;
		 int f2 = 1;
		 if (max == 1) {
			 System.out.println(f1);
		 }else if(max == 2) {
			 System.out.println(f1 + " " + f2);
		 }else {
			 System.out.print(f1 + " " + f2);
			 int sum = 0;
			 for(int i = 3; i <= max; i++) {
				 sum = f1 + f2;
				 System.out.print(" " + sum);
				 f1 = f2;
				 f2 = sum;
			 }
		 }
	}
}



le anh tuan [java1_online]
le anh tuan

2022-09-05 01:02:05



public class Fibonaci {
    public static void fibonaci(){
        Scanner scanner = new Scanner(System.in);
        System.out.print("Nhap so n: ");
        int n = scanner.nextInt();
        int f1 = 0;
        int f2 = 1;
        System.out.print(f1+" ");
        System.out.print(f2+" ");
        for (int i = 2; i < n; i++) {
            int temp = f1 + f2;
            System.out.print(temp + " ");
            f1 = f2;
            f2 = temp;
            
        }
    }

    public static void main(String[] args) {
       fibonaci();
    }
    
}



Nguyễn Anh Vũ [T2008A]
Nguyễn Anh Vũ

2021-02-24 09:30:38



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Fibonaci;

import static java.lang.Math.sqrt;
import java.text.DecimalFormat;
import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class Bt.Fibonaci {
    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        int n;
        System.out.println("Nhap so max : ");
        n = scan.nextInt();
        System.out.println("Day so Fobansi la : ");
        int F1 = 0;
        int F2 = 1;
        int Fn = 1;
        while(Fn <= n) {
            System.out.println("" + Fn);
            F1 = F2;
            F2 = Fn;
            Fn = F1 + F2;
        }
    }
}



Đặng Trần Nhật Minh [T2008A]
Đặng Trần Nhật Minh

2021-01-25 03:54:27


3.
package javalesson2;

import java.util.Scanner;

public class ex10 {
    
    public static int Fibonacci(int n) {
        
        int f0 = 0, f1 = 1, fn = 1;

        if (n < 0) return -1;
        else if (n == 0 || n == 1) return n;
        else {
            
            for (int i = 2; i < n; i++) {
            
                f0 = f1; f1 = fn; fn = f0 + f1;
                
            }
            
        }
        
        return fn;
        
    }

    public static void main(String[] args) {
        
        Scanner r = new Scanner(System.in);
        
        int maxx = r.nextInt(), n = 0;
        
        while (Fibonacci(n) <= maxx) System.out.println("F(" + n + ") = " + Fibonacci(n++));
        
    }

}



Đặng Trần Nhật Minh [T2008A]
Đặng Trần Nhật Minh

2021-01-25 03:29:22


1.
package javalesson2;

import java.util.Scanner;

public class ex09 {

    public static int Fibonacci (int n) {
        
        if (n < 3) return 1;
        else return Fibonacci(n - 1) + Fibonacci(n - 2);
        
    }
    
    public static void main(String[] args) {
        
        Scanner r = new Scanner(System.in);

        int maxx = r.nextInt(), n = 0;
        
        while (Fibonacci(n) < maxx) System.out.println("F(" + n + ") = " + Fibonacci(n++));

    }

}



hainguyen [T2008A]
hainguyen

2021-01-24 07:48:33



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package newpackage;

import static java.lang.Math.sqrt;
import java.text.DecimalFormat;
import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class NewClass {
    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        int n;
        System.out.println("Nhap so max : ");
        n = scan.nextInt();
        System.out.println("Day so Fobansi la : ");
        int F1 = 0;
        int F2 = 1;
        int Fn = 1;
        while(Fn <= n) {
            System.out.println("" + Fn);
            F1 = F2;
            F2 = Fn;
            Fn = F1 + F2;
        }
    }
}



Triệu Văn Lăng [T2008A]
Triệu Văn Lăng

2021-01-23 13:31:20



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bai1179;

import java.util.Scanner;

/**
 *
 * @author MTLS
 */
public class bt1179 {
    
    
    public static void main(String[] args) {
        
        int f0=0, f1=1, max, fn;
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap max = ");
        max = scan.nextInt();
        System.out.print(f0 + " " + f1 + " " + 1);
        fn = f0 + f1;
	while(fn <= max) {
		f0 = f1;
		f1 = fn;
		fn = f0 + f1;
                if(fn>max) {
                    break;
                }
                System.out.print(" " + fn + " ");
	}       
    }
}



vuong huu phu [T2008A]
vuong huu phu

2021-01-22 12:25:29



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg2_bai_1;

import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class bai_3 {
     public static void main(String[] args) {
     int a ,a1=0, a2=1,a3,i;
     Scanner scan = new Scanner(System.in);
     System.out.println("nhap so phan tu cua day: ");
     a = scan.nextInt();
for (i =0; i<a ;i++){
a3 = a1+a2;
System.out.format(" %d ",a3);
a1=a2;
a2=a3;
}
     }
}



Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2021-01-22 09:21:17



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson2;

import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class Fibonacci {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int max;
        max = scan.nextInt();
        int a = 1 , b = 1, tong = 0;
        System.out.print("1 1 ");
        while(true){
            tong = a + b;
            if( tong > max ) break;
            System.out.print(tong +" ");
            a = b;
            b = tong;
        }
    }
}



Trần Thị Khánh Huyền [T2008A]
Trần Thị Khánh Huyền

2021-01-22 08:31:04



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package chuoifibonaci;
import java.util.Scanner;
/**
 *
 * @author Admin
 */
public class ChuoiFibonaci {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    Scanner scan = new Scanner(System.in);
    
    int n;
    System.out.println("Nhap vao số max: ");
    n = scan.nextInt();
    System.out.print("Day so Fibonaci la: 1");
    int F1=0;
    int F2=1;
    int Fn=1;
    while(Fn<=n){
        System.out.print(", "+Fn+" ");
        F1 = F2;
        F2 = Fn;
        Fn = F1+F2;
        
    }
    }
    
}