我如何将斐波那契数列存储在矩阵中
how can i store fibonacci sequence in a matrix
好吧,我需要将斐波那契数列存储在 2x5 矩阵中,但我所做的不起作用。这是我的尝试
int main()
{
int i,j,k;
int mat[2][5];
mat[0][0]=0;
mat[0][1]=1;
k=2;
for (i=0; i<2; i++)
{
for(j=0; j<5; j++)
{
mat[i][k]=mat[i][j]+mat[i][j+1];
k++;
}
}
for(i=0; i<2; i++)
{
for(j=0; j<5; j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
return 0;
}
如何使用矩阵的行和列来计算该元素在斐波那契数列中的“位置”?
#include <stdio.h>
#define ROWS 2
#define COLS 5
int fibonacci(int position)
{
if (position == 0)
return 0;
if (position == 1)
return 1;
return fibonacci(position - 1) + fibonacci(position - 2);
}
int main()
{
int mat[ROWS][COLS];
for(int row = 0; row < ROWS; row++)
{
for(int col = 0; col < COLS; col++)
{
/*
[0,0] = 0 [0,1] = 1, [0,2] = 2, [0,3] = 3, [0,4] = 4
[1,0] = 5 [1,1] = 6, [1,2] = 7, [1,3] = 8, [1,4] = 9
*/
mat[row][col] = fibonacci(COLS * row + col);
printf("%3d, ", mat[row][col]);
}
printf("\n");
}
return 0;
}
请您尝试以下操作:
#include <stdio.h>
int main()
{
int i, j, k;
int mat[2][5] = {{0, 1}};
for (k = 2; k < 10; k++) {
mat[k / 5][k % 5] = mat[(k - 1) / 5][(k - 1) % 5] + mat[(k - 2) / 5][(k - 2) % 5];
}
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
return 0;
}
好吧,我需要将斐波那契数列存储在 2x5 矩阵中,但我所做的不起作用。这是我的尝试
int main()
{
int i,j,k;
int mat[2][5];
mat[0][0]=0;
mat[0][1]=1;
k=2;
for (i=0; i<2; i++)
{
for(j=0; j<5; j++)
{
mat[i][k]=mat[i][j]+mat[i][j+1];
k++;
}
}
for(i=0; i<2; i++)
{
for(j=0; j<5; j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
return 0;
}
如何使用矩阵的行和列来计算该元素在斐波那契数列中的“位置”?
#include <stdio.h>
#define ROWS 2
#define COLS 5
int fibonacci(int position)
{
if (position == 0)
return 0;
if (position == 1)
return 1;
return fibonacci(position - 1) + fibonacci(position - 2);
}
int main()
{
int mat[ROWS][COLS];
for(int row = 0; row < ROWS; row++)
{
for(int col = 0; col < COLS; col++)
{
/*
[0,0] = 0 [0,1] = 1, [0,2] = 2, [0,3] = 3, [0,4] = 4
[1,0] = 5 [1,1] = 6, [1,2] = 7, [1,3] = 8, [1,4] = 9
*/
mat[row][col] = fibonacci(COLS * row + col);
printf("%3d, ", mat[row][col]);
}
printf("\n");
}
return 0;
}
请您尝试以下操作:
#include <stdio.h>
int main()
{
int i, j, k;
int mat[2][5] = {{0, 1}};
for (k = 2; k < 10; k++) {
mat[k / 5][k % 5] = mat[(k - 1) / 5][(k - 1) % 5] + mat[(k - 2) / 5][(k - 2) % 5];
}
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
return 0;
}