C++ 实现斐波那契数列求模算法
#include
int fibonacci(int n) { if (n == 1 || n == 2) { return 1; } int a = 1; int b = 1; int c; for (int i = 3; i <= n; i++) { c = (a + b) % 1000; a = b; b = c; } return c; }
int main() { int n; cin >> n; int *a = new int[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cout << fibonacci(a[i]) << endl; } delete[] a; return 0; }
原文地址: https://www.cveoy.top/t/topic/p4OV 著作权归作者所有。请勿转载和采集!