楊英翔 許崇
摘 要:作者多年從事計算機應(yīng)用技術(shù)研究和教學(xué),在C語言程序設(shè)計的教學(xué)過程中,作者一直在探索激發(fā)學(xué)生學(xué)習(xí)興趣、降低課程難度、提高學(xué)習(xí)成績的有效途徑,并提出自己的一些做法供同行們參考。
關(guān)鍵詞:C語言;教法;計算機
1 用數(shù)組的方法求斐波那契數(shù)列前20項
程序說明:
”斐波那契數(shù)列”是C語言里很典型的一道題,可以用多種方法解答,本書前文就有用循環(huán)求解的方法。
練習(xí)方法:
(1).程序原型:
#include
#include
#include
#include
main()
{
system(“color 2f”);
system("title ---斐波那契數(shù)列");
system(“mode con cols=130 lines=40”);
int f[256]={1,1},i,j,t,k,s;
for(i=2;i<20;i++)
{
f[i]=f[i-1]+f[i-2];
}
printf("\n斐波那契數(shù)列—————\n");
for(i=0;i<20;i++)
{
printf(“%15d”,f[i]);
if((i+1)%4==0)
printf(“\n”);
}
getchar();
}
(2).參考程序:有一分數(shù)序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個數(shù)列的前20項之和。
#include
#include
main()
{
float a1[255]={2,3};
float a2[255]={1,2};
float a3[255];
int i;
float s=0;
for (i=2;i<20;i++)
{
a1[i]=a1[i-1]+a1[i-2];
a2[i]=a2[i-1]+a2[i-2];
}
for(i=0;i<20;i++)
{
a3[i]=a1[i]/a2[i];
s+=a3[i];
}
printf("\n數(shù)列和為 %1.2f\n\n ",s);
getchar();
}
(3).程序變形:
#include
#include
main()
{
int n,t,number=20;
float a=2,b=1,s=0;
for(n=1;n<=number;n++)
{
s=s+a/b;
t=a;a=a+b;b=t; //這部分是程序的關(guān)鍵,請讀者猜
猜t的作用
}
printf(“sum is %9.6f\n”,s);
system(“pause”);
}
后記:
數(shù)組的靈活應(yīng)用的題目很多,教師在這部分教學(xué)中應(yīng)該多講一些例題。拓展學(xué)生的知識面。
2 解方程
一個整數(shù),它加上100后是一個完全平方數(shù),再加上168又是一個完全平方數(shù),請問該數(shù)是多少??
程序說明:
這個題目里我們使用了嵌套循環(huán)和goto語句。
這個題目可以用多個算法,程序原型里的算法屬于暴力破解法,又叫窮舉法。
練習(xí)方法:
(1).程序原型:
#include
#include
#include
#include
void main()
{
system(“color 3f”);
system(“title 關(guān)于完全平方數(shù)”);
system(“mode con cols=130 lines=20”);
int i,j,k;
for (i=1;i<=1000;i++)
{
for (j=1;j<=1000;j++)
{
for (k=1;k<=1000;k++)
{
if(i*i==k+100&&j;*j==k+268)
{
printf(“\n\n 這個整數(shù)可以是
%d\n\n “,k);
goto lop;
}
}
}
}
lop:
printf(“\n\n “);
}
(2).程序變形:
#include
#include
main()
{
long int i,x,y,z;
for (i=1;i<100000;i++)
{
x=sqrt(i+100); //x為加上100后開方后的結(jié)果
y=sqrt(i+268); //y為再加上168后開方后的結(jié)果
if(x*x==i+100&&y;*y==i+268) //判斷此數(shù)是完全平方數(shù)
printf(“\n%ld\n\n”,i);
}
getchar();
}
作者簡介
楊英翔,沈陽建筑大學(xué),副教授。
許崇,沈陽建筑大學(xué),實驗師。