1、应用1: 利用数组求Fibonacci数列的前20项
问题及代码:
#include <stdio.h>
int main( )
{
int i;
int f[20]= {1,1};//先将初始的两个单独声明定义
for(i=2; i<20; i++)
f[i]=f[i-2]+f[i-1];
for(i=0; i<20; i++)
{
if((i+1)%5==0)
printf("\n");
printf("%8d", f[i]);
}
return 0;
}
运行结果:
知识总结:
用数组进行斐波那数列的求和运算。
学习心得:
先将与规律不同的前两个数单独定义,之后再用公式运算;
2、应用2: 谁最大——有若干数字存入数组中,请找出最大值
#include <stdio.h>
#define SIZE 5
int main()
{
int d[SIZE], i, max, index;
printf("Input %d numbers: ", SIZE);
for(i=0; i<SIZE; i ++)
scanf("%d", &d[i]);
max=d[0];
index=0;
for(i=1; i<SIZE; i++)
if(d[i] > max)
{
max = d[i];
index = i;
}
printf("The max number is %d .\n", max);
printf("The index of the max number is %d .\n", index);
return 0;
}
运行结果:
知识总结:
求数组最大值的方法,运用到最大值的赋值;
学习心得:
个人认为index初始应赋值为1,可为何贺老师要赋值为0呢?
3、应用3 利用数组输出十进制数对应的二进制数
#include <stdio.h>
int main()
{
int x, i, n ;
int a[100];
scanf("%d", &x); //x是要转换的数
i=0;
while(x) //while(x!=0)
{
a[i]=x%2;
x=x/2;
i++;
}
n=i;
for(i=n-1; i>=0; i--)
printf("%d", a[i]);
printf("\n");
return 0;
}
运行结果:
知识总结:
可以通用到各种进制的转换;
学习心得:
这题还不是很懂。