One-dimensional array program example
Using one-dimensional array processing to solve Fibonacci sequence problem
#include <stdio.h>
int main()
{
int i;
int f[20]={1,1}; //对前面最两个的0 1 赋值为1
for(i=2;i<20;i++)
f[i]=f[i-2]+f[i-1]; //先后求出f【2】到发【19】的值
for(i=0;i<20;i++)
{
if(i%5==0) printf("\n"); //控制每输出五个数后换行
printf("%12d",f[i]); //输出每一个数
}
printf("\n") ;
return 0;
}
operation result
Program analysis : define the length of the array as 20, and specify the initial value as 1 for the first two numbers f [0] and f [1]. According to the characteristics of the sequence, the value of the first two elements can be calculated as the third The value of the element f [2] = f [1] + f [0];
in a sequential loop, you can use the following loop to express the value of f [2] —— f [9]. f [i] = f [i-2] + f [i-1]; if statement is used to control line feed, 5 digits per line