Wprowadzenie do serii Fibonacciego w C ++
Seria Fibonacciego to seria liczb. Tworzy łańcuch liczb dodając dwie ostatnie cyfry. Obliczanie serii Fibonacciego jest łatwe, ponieważ musimy tylko dodać dwie ostatnie cyfry, aby uzyskać kolejną cyfrę. Ale w pewnym momencie, gdy liczba cyfr staje się duża, staje się dość złożona. Aby rozwiązać ten problem, zobaczymy, jak możemy stworzyć program serii Fibonacciego w języku C ++, aby stało się to łatwe. Zobaczmy, jak naprawdę działa seria Fibonacciego.
Niech f (n) będzie n-tym terminem.
f (0) = 0;
f (1) = 1;
Seria będzie następująca:
1 (1 + 0)
2 (1 + 1)
3 (1 + 2)
5 (2 + 3)
8 (3 + 5)
13 (5 + 8)
21 (8 + 13
…i tak dalej
Logika tworzenia serii Fibonacciego
- Zainicjuj pierwszą liczbę na 0
- Zainicjuj drugą liczbę na 1
- Dodaj pierwszą i drugą liczbę.
- Zapisz wartość dodania w trzeciej liczbie.
- Wydrukuj trzeci numer.
- Przypisz drugi numer do pierwszego numeru
- Przypisz trzeci numer do drugiego numeru.
- Powtarzaj kroki od 3 do 7, aż obliczona zostanie seria Fibonacciego dla danej liczby.
Programy serii Fibonacciego w C ++
W tej sekcji zobaczymy, jak znaleźć serię Fibonacciego przy użyciu różnych metod, takich jak rekurencja, pętla, tablica, bez rekurencji itp .:
1. Bez metody rekurencji
Kod:
#include
using namespace std;
int main()
(
int first_num = 0, second_num = 1, third_num, i, num;
cout << "Enter random number to print fibonacci series:";
cin >> num;
cout << "Fibonacci Series for a given number:" < cout << first_num < cout << second_num << endl;
for(i = 2; i < num; ++i) //loop will starts from 2 because we have printed 0 and 1 before
(
third_num = first_num + second_num;
cout << third_num << endl;
first_num = second_num;
second_num = third_num;
)
return 0;
)#include
using namespace std;
int main()
(
int first_num = 0, second_num = 1, third_num, i, num;
cout << "Enter random number to print fibonacci series:";
cin >> num;
cout << "Fibonacci Series for a given number:" < cout << first_num < cout << second_num << endl;
for(i = 2; i < num; ++i) //loop will starts from 2 because we have printed 0 and 1 before
(
third_num = first_num + second_num;
cout << third_num << endl;
first_num = second_num;
second_num = third_num;
)
return 0;
)#include
using namespace std;
int main()
(
int first_num = 0, second_num = 1, third_num, i, num;
cout << "Enter random number to print fibonacci series:";
cin >> num;
cout << "Fibonacci Series for a given number:" < cout << first_num < cout << second_num << endl;
for(i = 2; i < num; ++i) //loop will starts from 2 because we have printed 0 and 1 before
(
third_num = first_num + second_num;
cout << third_num << endl;
first_num = second_num;
second_num = third_num;
)
return 0;
)
Wynik:
2. Przy użyciu metody rekurencji
Kod:
#include
using namespace std;
void FibonacciSeries(int num)
(
static int first_num = 0, second_num = 1, third_num;
if(num > 0)
(
third_num = first_num + second_num;
first_num = second_num;
second_num = third_num;
cout << third_num << endl;
FibonacciSeries(num - 1);
)
)
int main()
(
int num;
cout << "Enter random number to print fibonacci series:";
cin >> num;
cout << "Fibonacci Series for a given number: \n" << endl;
cout << "0" << endl;
cout << "1" << endl;
FibonacciSeries (num - 2); //number-2 is used because we have already print 2 numbers
return 0;
)
Wynik:
3. Korzystanie z pętli For
Kod:
#include
using namespace std;
int main()
(
int first_num= 0, second_num = 1, third_num, i, num;
cout << "Enter random number to print fibonacci series:";
cin >> num;
cout << "Fibonacci Series for a given number:" < cout << first_num < cout << second_num < for(i = 0; i < num; i++) //loop will starts from 2 because we have printed 0 and 1 before
(
if(i <= 1)
third_num = i;
else
(
third_num = first_num + second_num;
cout << third_num < first_num = second_num;
second_num = third_num;
)
)
return 0;
)#include
using namespace std;
int main()
(
int first_num= 0, second_num = 1, third_num, i, num;
cout << "Enter random number to print fibonacci series:";
cin >> num;
cout << "Fibonacci Series for a given number:" < cout << first_num < cout << second_num < for(i = 0; i < num; i++) //loop will starts from 2 because we have printed 0 and 1 before
(
if(i <= 1)
third_num = i;
else
(
third_num = first_num + second_num;
cout << third_num < first_num = second_num;
second_num = third_num;
)
)
return 0;
)#include
using namespace std;
int main()
(
int first_num= 0, second_num = 1, third_num, i, num;
cout << "Enter random number to print fibonacci series:";
cin >> num;
cout << "Fibonacci Series for a given number:" < cout << first_num < cout << second_num < for(i = 0; i < num; i++) //loop will starts from 2 because we have printed 0 and 1 before
(
if(i <= 1)
third_num = i;
else
(
third_num = first_num + second_num;
cout << third_num < first_num = second_num;
second_num = third_num;
)
)
return 0;
)#include
using namespace std;
int main()
(
int first_num= 0, second_num = 1, third_num, i, num;
cout << "Enter random number to print fibonacci series:";
cin >> num;
cout << "Fibonacci Series for a given number:" < cout << first_num < cout << second_num < for(i = 0; i < num; i++) //loop will starts from 2 because we have printed 0 and 1 before
(
if(i <= 1)
third_num = i;
else
(
third_num = first_num + second_num;
cout << third_num < first_num = second_num;
second_num = third_num;
)
)
return 0;
)#include
using namespace std;
int main()
(
int first_num= 0, second_num = 1, third_num, i, num;
cout << "Enter random number to print fibonacci series:";
cin >> num;
cout << "Fibonacci Series for a given number:" < cout << first_num < cout << second_num < for(i = 0; i < num; i++) //loop will starts from 2 because we have printed 0 and 1 before
(
if(i <= 1)
third_num = i;
else
(
third_num = first_num + second_num;
cout << third_num < first_num = second_num;
second_num = third_num;
)
)
return 0;
)
Wynik:
4. Korzystając z pętli While
Kod:
#include
using namespace std;
int main()
(
int first_num = 0, second_num = 1, third_num = 0, i = 3, num;
cout << "Enter random number to print fibonacci series:" ;
cin >> num;
cout << "Fibonacci Series for a given number:" < cout << first_num < cout << second_num < while(i <= num)
(
third_num = first_num + second_num;
cout << third_num < first_num = second_num;
second_num = third_num;
i = i + 1;
)
return 0;
)#include
using namespace std;
int main()
(
int first_num = 0, second_num = 1, third_num = 0, i = 3, num;
cout << "Enter random number to print fibonacci series:" ;
cin >> num;
cout << "Fibonacci Series for a given number:" < cout << first_num < cout << second_num < while(i <= num)
(
third_num = first_num + second_num;
cout << third_num < first_num = second_num;
second_num = third_num;
i = i + 1;
)
return 0;
)#include
using namespace std;
int main()
(
int first_num = 0, second_num = 1, third_num = 0, i = 3, num;
cout << "Enter random number to print fibonacci series:" ;
cin >> num;
cout << "Fibonacci Series for a given number:" < cout << first_num < cout << second_num < while(i <= num)
(
third_num = first_num + second_num;
cout << third_num < first_num = second_num;
second_num = third_num;
i = i + 1;
)
return 0;
)#include
using namespace std;
int main()
(
int first_num = 0, second_num = 1, third_num = 0, i = 3, num;
cout << "Enter random number to print fibonacci series:" ;
cin >> num;
cout << "Fibonacci Series for a given number:" < cout << first_num < cout << second_num < while(i <= num)
(
third_num = first_num + second_num;
cout << third_num < first_num = second_num;
second_num = third_num;
i = i + 1;
)
return 0;
)#include
using namespace std;
int main()
(
int first_num = 0, second_num = 1, third_num = 0, i = 3, num;
cout << "Enter random number to print fibonacci series:" ;
cin >> num;
cout << "Fibonacci Series for a given number:" < cout << first_num < cout << second_num < while(i <= num)
(
third_num = first_num + second_num;
cout << third_num < first_num = second_num;
second_num = third_num;
i = i + 1;
)
return 0;
)
Wynik:
5. Korzystanie z tablicy
Kod:
#include
using namespace std;
int main()
(
int fibonacci(25), i, num;
cout << "Enter random number to print fibonacci series:";
cin >> num;
fibonacci(0) = 0;
fibonacci(1) = 1;
for (i = 2; i < num; i++)
(
fibonacci(i) = fibonacci(i - 1) + fibonacci(i - 2);
)
cout <<"Fibonacci Series for a given number:" << endl;
for (i = 0; i < num; i++)
(
cout << fibonacci(i) < )
return 0;
)#include
using namespace std;
int main()
(
int fibonacci(25), i, num;
cout << "Enter random number to print fibonacci series:";
cin >> num;
fibonacci(0) = 0;
fibonacci(1) = 1;
for (i = 2; i < num; i++)
(
fibonacci(i) = fibonacci(i - 1) + fibonacci(i - 2);
)
cout <<"Fibonacci Series for a given number:" << endl;
for (i = 0; i < num; i++)
(
cout << fibonacci(i) < )
return 0;
)
Wynik:
6. Program szeregowy Fibonacciego w C ++ z wykorzystaniem określonej liczby
Kod:
#include
using namespace std;
int main()
(
int first_num = 0, second_num = 1, third_num, i;
cout << "Fibonacci Series for a given number:";
cout << first_num < cout << second_num < for(i = 2; i < 10; ++i) //loop will starts from 2 because we have printed 0 and 1 before
(
third_num = first_num + second_num;
cout << third_num < first_num = second_num;
second_num = third_num;
)
return 0;
)#include
using namespace std;
int main()
(
int first_num = 0, second_num = 1, third_num, i;
cout << "Fibonacci Series for a given number:";
cout << first_num < cout << second_num < for(i = 2; i < 10; ++i) //loop will starts from 2 because we have printed 0 and 1 before
(
third_num = first_num + second_num;
cout << third_num < first_num = second_num;
second_num = third_num;
)
return 0;
)#include
using namespace std;
int main()
(
int first_num = 0, second_num = 1, third_num, i;
cout << "Fibonacci Series for a given number:";
cout << first_num < cout << second_num < for(i = 2; i < 10; ++i) //loop will starts from 2 because we have printed 0 and 1 before
(
third_num = first_num + second_num;
cout << third_num < first_num = second_num;
second_num = third_num;
)
return 0;
)#include
using namespace std;
int main()
(
int first_num = 0, second_num = 1, third_num, i;
cout << "Fibonacci Series for a given number:";
cout << first_num < cout << second_num < for(i = 2; i < 10; ++i) //loop will starts from 2 because we have printed 0 and 1 before
(
third_num = first_num + second_num;
cout << third_num < first_num = second_num;
second_num = third_num;
)
return 0;
)
Wynik:
Wniosek
W tym artykule widzieliśmy, jak wygenerować serię Fibonacciego w języku C ++ przy użyciu różnych metod. Mam nadzieję, że ten artykuł okaże się pomocny.
Polecane artykuły
To jest przewodnik po serii Fibonacciego w C ++. Tutaj omawiamy różne programy z serii Fibonacciego w C ++. Możesz również przejrzeć nasze inne powiązane artykuły, aby dowiedzieć się więcej-
- Scal sortowanie algorytmów w Javie
- ASP.NET Framework
- Program Palindrome in C.
- PHP Do While Loop
- Wprowadzenie do serii Fibonacciego w C #
- Seria Fibonacciego w Javie
- Palindrom w C ++
- Algorytmy szybkiego sortowania w Javie
- Do While Loop w JavaScript