By GokiSoft.com| 14:08 29/11/2021|
Lập Trình C

[Video] Hướng dẫn giải bài Fibonacci - Lập trình C

Fibonacci - Lập trình C



#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 = 1, f1 = 1, fn, max;
	printf("\nNhap so max = ");
	scanf("%d", &max);
	
	//Bat dau in day fibonacci
	printf("\n%d, %d, ", f0, f1);
	
	while(1) {
		fn = f0 + f1;
		if(fn > max) {
			break;
		}
		
		printf("%d, ", fn);
		f0 = f1;
		f1 = fn;
	}
	
	//Cach 2: in day fibonacci
	f0 = 1;
	f1 = 1;
	
	printf("\n%d, %d, ", f0, f1);
	fn = f0 + f1;
	while(fn <= max) {
		printf("%d, ", fn);
		
		f0 = f1;
		f1 = fn;
		fn = f0 + f1;
	}
	
	//Cach 3: in day fibonacci
	f0 = 1;
	fn = 1;
	printf("\n%d, ", f0);
	while(fn <= max) {
		printf("%d, ", fn);
		
		fn += f0;
		f0 = fn - f0;
	}
	
	return 0;
}




Tags:

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

5

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