我想列印最多 50 個項的斐波那契數列,但是當 n 為 50 時它不起作用,直到 n <= 48。有沒有辦法做到這一點?
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
unsigned long long arr[50] = {0, 1};
for (int i = 0, j = 2; i < n; i , j )
{
arr[j] = arr[i] arr[i 1];
}
for (int i = 0; i < n; i )
{
printf("%d => %llu\n", i, arr[i]);
}
return 0;
}
uj5u.com熱心網友回復:
那么問題解決了我在回圈條件中犯了錯誤所以這里是更新的代碼。
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
unsigned long long arr[50] = {0, 1};
for (int i = 2; i < n; i )
{
arr[i] = arr[i - 1] arr[i - 2];
}
for (int i = 0; i < n; i )
{
printf("%llu ", arr[i]);
}
return 0;
}
uj5u.com熱心網友回復:
#include <stdio.h>
int main() {
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 t2;
}
return 0;
}
uj5u.com熱心網友回復:
void printFibonacci(int n)
{
static int n1=0,n2=1,n3;
if(n>0)
{
n3 = n1 n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/488053.html
下一篇:C從檔案中決議資料
