By GokiSoft.com| 20:05 18/09/2023|
Lập Trình C

Fibonacci - Lập trình C BT1883

Cho chuỗi Fibonacci như sau

F(0) = 1;

F(1) = 1;

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

Nhập vào số Fibonaci max từ bàn phím

Yêu cầu : In ra chuỗi fibonaci với số lớn nhất là max

Ví dụ :

max = 100; được nhập từ bàn phím

Chuỗi fibonaci sẽ như sau

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89

Liên kết rút gọn:

https://gokisoft.com/1883

Bình luận

avatar
TRẦN VĂN ĐIỆP [community,C1812L,C2002L,T2008A]
2020-12-16 02:31:57



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	
	int max;
	printf("\nNhap max = ");
	scanf("%d", &max);
	
	int f0 = 1, f1 = 1, fn;
	
	printf("\n1, 1");
	while(1) {
		fn = f0 + f1;
		if(fn > max) {
			break;
		}
		printf(", %d", fn);
		f0 = f1;
		f1 = fn;
	}
	
	return 0;
}


avatar
Nguyễn Anh Vũ [T2008A]
2020-09-22 18:00:05



#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
      //bat dau code;
	
	int FMax, x=0,y=1,F;
	printf("\nNhap max :");
	scanf("%d",&FMax);
	while(1){
		F=x+y;
		if(F>FMax) {
			break;
		}else{
			printf("\n%d",F);
			x=y;
			y=F;
			
	    }
	}
	

       //ket thuc code;
	return 0;
}


avatar
An Văn Minh [T2008A]
2020-09-17 13:16:08



#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) 
{
	int f0, f1, fn, max;
	printf("\nNhap so Fibonacci max = "); scanf("%d", &max);
	f0=1;
	f1=1;
	printf("1");
	while(fn<=max){
		f0=f1;
		f1=fn;
	printf(", %d",fn);
		fn=f1+f0;
	}
	return 0;
}


avatar
Trần Thị Khánh Huyền [T2008A]
2020-09-17 12:30:09



#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int f0, f1, fn, max;
	printf("Nhap so Fibonacci max = "); scanf("%d", &max);
	f0=1;
	f1=1;
	printf("1");
	while(fn<=max){
		f0=f1;
		f1=fn;
	printf(", %d",fn);
		fn=f1+f0;
	
	}
	
	
	return 0;
}


avatar
Trần Văn Lâm [T2008A]
2020-09-17 02:43:12



#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int f0, f1, fn, max;
	f0 = 1;
	f1 = 1;
	printf("\nNhap max: ");
	scanf("%d", &max);
	
	printf("\n%d, %d", f0, f1);
	
	while(1) {
		fn = f0 + f1;
		if(fn > max) {
			break;
		}
		printf(", %d", fn);
		f0 = f1;
		f1 = fn;
	}
	return 0;
}


avatar
hainguyen [T2008A]
2020-09-16 07:08:30



#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int f0, f1, fn, max;
	
	f0 = 1;
	f1 = 1;
	
	printf("\nNhap max: ");
	scanf("%d", &max);
	
	printf("\n%d, %d", f0, f1);
	
	while(1){
		fn = f1 + f0; 
		if(fn > max)
		break;
		
	}
	printf(", %d", fn);
	f0 = f1;
	f1 = fn;
	
	return 0;
}


avatar
TRẦN VĂN ĐIỆP [Teacher]
2020-09-16 06:59:30



Cấu trúc dữ liệu và giải thuật
	- Giải thuật sắp xếp
	- Tìm kiếm
	- Vun đống
	- Cây nhị phân tìm kiếm
	- Bài toán tham lam
	- Giải thuật dijkstra
	- Giải thuật tìm kiếm đường đi


avatar
TRẦN VĂN ĐIỆP [Teacher]
2020-09-16 06:56:15



#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int f0, f1, fn, max;
	//khoi tao du lieu
	f0 = 1;
	f1 = 1;
	
	printf("\nNhap max: ");
	scanf("%d", &max);
	
	printf("\n%d, %d", f0, f1);
	
//	while(1) {
//		fn = f0 + f1;
//		if(fn > max) {
//			break;
//		}
//		printf(", %d", fn);
//		f0 = f1;
//		f1 = fn;
//	}
	fn = f0 + f1;
	while(fn <= max) {
		printf(", %d", fn);
		f0 = f1;
		f1 = fn;
		fn = f0 + f1;
	}
	
	return 0;
}


avatar
TRẦN VĂN ĐIỆP [Teacher]
2020-09-16 06:52:43

- Bài toán Fibonaci

- Tìm số Fibonaci ở vị trí n (n nhập từ bàn phím)

- Tính tổng các số Fibonaci chia hết cho 2 trong dãy Fibonaci có số max là n (n -> nhập từ bàn phím)

avatar
TRẦN VĂN ĐIỆP [Teacher]
2020-09-16 06:48:37



#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int f0, f1, fn, max;
	//khoi tao du lieu
	f0 = 1;
	f1 = 1;
	
	printf("\nNhap max: ");
	scanf("%d", &max);
	
	printf("\n%d, %d", f0, f1);
	
	while(1) {
		fn = f0 + f1;
		if(fn > max) {
			break;
		}
		printf(", %d", fn);
		f0 = f1;
		f1 = fn;
	}
	
	return 0;
}