标签:cout lib space one alt names std pen i++
#include <iostream> #include <cstdio> #include <stdlib.h> #include <algorithm> using namespace std; int main() { int a[120]; int k, m; while (1) { cout << "输入阶数k和约定的常数max。k和max用空格分开。" << endl; cin >> k >> m; int i; for (i = 0; i < k - 1; i++) a[i] = 0; a[k - 1] = 1; a[k] = 1; int n = k + 1; if (m == 0) { cout << k - 2 << endl; cout << 1 << endl; return 0; } if (m == 1) { cout << k << endl; cout << 1 << endl; return 0; } while (a[n - 1] <= m) { a[n] = 2 * a[n - 1] - a[n - k - 1]; n++; } cout << n - 2 << endl; cout << a[n - 2] << endl; } return 0; }
题目:
用循环队列编写求k阶斐波那契序列中前n+1项(f1,f2,…,fn)的算法,要求满足fn≤(小于等于)max,而fn +1>max
max为某个约定的常数。注意:本题所用循环队列的容量为k,算法结束时,留在队列中的元素为所求k阶斐波那契序列中的最后k项
思路:
已知K阶斐波那契数列定义为: f0 = 0,f1 = 0, … ,fk-2 = 0, fk-1 = 1;
并且fn = fn-1 + fn-2 + … + fn-k , n = k , k + 1, …
标签:cout lib space one alt names std pen i++
原文地址:http://www.cnblogs.com/luckyraye/p/6715362.html