Въведение в сериите на Фибоначи в C ++
Серията Фибоначи е поредица от числа. Той прави веригата от числа, добавяща последните две числа. Изчисляването на серията Фибоначи е лесно, тъй като ние трябва просто да добавим последната двуцифрена, за да получим друга цифра. Но в един момент, когато броят на цифрите стане по-голям, той става доста сложен. И така, за да разрешим този проблем, ще видим как можем да създадем програма от серии на Фибоначи на езика C ++, така че да стане лесно. Нека да видим как действително работи серията на Фибоначи.
Нека f (n) е n-ти член.
F (0) = 0;
е (1) = 1;
Серията ще бъде следната:
1 (1 + 0)
2 (1 + 1)
3 (1 + 2)
5 (2 + 3)
8 (3 + 5)
13 (5 + 8)
21 (8 + 13)
…и така нататък
Логиката зад генерирането на серия Фибоначи
- Инициализирайте първото число до 0
- Инициализирайте второто число до 1
- Добавете първото и второто число.
- Съхранявайте стойността на добавянето в третото число.
- Отпечатайте третото число.
- Присвойте второто число към първото число
- Присвойте третото число към второто число.
- Повторете стъпка 3 до стъпка 7, докато се изчисли серията на Фибоначи за дадено число.
Програми от серията Фибоначи в C ++
В този раздел ще видим как да разберем серията Фибоначи, използвайки различни методи като рекурсия, цикъл, масив, без рекурсия и т.н.:
1. Без метода на рекурсия
Код:
#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;
)
изход:
2. Чрез използване на рекурсионен метод
Код:
#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;
)
изход:
3. Използване на цикъл For
Код:
#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;
)
изход:
4. Използвайки while Loop
Код:
#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;
)
изход:
5. Използване на масив
Код:
#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;
)
изход:
6. Програма от серията Фибоначи в C ++, като се използва зададен номер
Код:
#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;
)
изход:
заключение
В тази статия видяхме как да се генерират серии на Фибоначи на език C ++, използвайки различни методи. Надявам се, че ще намерите тази статия полезна.
Препоръчителни статии
Това е ръководство за сериите на Фибоначи в C ++. Тук обсъждаме различните програми от серията Фибоначи в C ++. Можете също да разгледате и другите ни свързани статии, за да научите повече-
- Обединяване на алгоритми за сортиране в Java
- ASP.NET Framework
- Палиндром в програма C
- PHP Правете докато цикъл
- Въведение в сериите на Фибоначи в C #
- Серия Фибоначи в Java
- Палиндром в C ++
- Бързо сортиране на алгоритми в Java
- Правете докато Loop в JavaScript