fortran程序案例题汇编(14道)

合集下载

fortran程序案例题汇编(14道)

fortran程序案例题汇编(14道)

1.Fibonacci数列定义如下:F1=1F2=1Fn=Fn-1+Fn-2(n>2)求Fibonacci数列的前30项。

integer f(30),if(1)=1f(2)=2do i=3,30f(i)=f(i-1)+f(i-2)enddoprint*,fend2.输入10个学生的总分,求每个学生的名次integer s(10),a(10),i,jdo i=1,10read*,s(i)enddodo i=1,10a(i)=1do j=1,10if(s(i)<s(j))a(i)=a(i)+1enddoenddodo i=1,10print*,s(i),a(i)enddoend3.给定一组数,按照从小到大的顺序输出。

integer a(10)integer pdo i=1,10read *,a(i)enddodo j=1,9p=jdo i=j+1,10if (a(i)<a(p))p=ienddoif (p/=j) thent=a(p);a(p)=a(j);a(j)=tendifenddoprint *,(a(i),i=1,10)end4.输入若干名学生的学号和三门课程(语数英)的成绩,要求从键盘输入一个学生的学号,能打印出该学生的三门功课成绩和总分。

character*6,dimension(:),allocatable::xueinteger,dimension(:,:),allocatable::ginteger,dimension(:),allocatable::zonginteger i,j,ncharacter*6,xhprint *,"请输入学生的个数"read *,nallocate(xue(n))allocate(g(n,3))allocate(zong(n))do i=1,nread *,xue(i),(g(i,j),j=1,3)enddodo i=1,nzong(i)=0do j=1,3zong(i)=zong(i)+g(i,j)enddoenddoprint *,"请输入你要打印的学生的学号"read *,xhdo i=1,nif(xue(i)==xh)thenprint *,(g(i,j),j=1,3),zong(i)exitendifenddoend5.编写一个函数子程序计算所输入两个整数m、n的最大公约数。

fortran编程习题答案

fortran编程习题答案

fortran编程习题答案Fortran编程习题答案Fortran是一种古老而强大的编程语言,广泛应用于科学计算和工程领域。

在学习Fortran编程的过程中,解决习题是一种非常有效的方法。

本文将为您提供一些Fortran编程习题的答案,帮助您更好地理解和掌握这门语言。

1. 习题一:编写一个Fortran程序,计算并输出1到100之间所有整数的平方。

程序代码如下:```fortranprogram squareimplicit noneinteger :: ido i = 1, 100print *, i, i**2end doend program square```2. 习题二:编写一个Fortran程序,计算并输出斐波那契数列的前20个数。

程序代码如下:```fortranprogram fibonacciimplicit noneinteger :: i, n, fib(20)fib(1) = 0fib(2) = 1do i = 3, 20fib(i) = fib(i-1) + fib(i-2)end dodo i = 1, 20print *, fib(i)end doend program fibonacci```3. 习题三:编写一个Fortran程序,计算并输出一个给定整数的阶乘。

程序代码如下:```fortranprogram factorialimplicit noneinteger :: i, n, resultprint *, "请输入一个整数:"read *, nresult = 1do i = 1, nresult = result * iend doprint *, n, "的阶乘为:", resultend program factorial```4. 习题四:编写一个Fortran程序,计算并输出一个给定整数是否为素数。

程序代码如下:```fortranprogram primeimplicit noneinteger :: i, n, countprint *, "请输入一个整数:"read *, ncount = 0do i = 2, n-1if (mod(n, i) == 0) thencount = count + 1end ifend doif (count == 0) thenprint *, n, "是素数"elseprint *, n, "不是素数"end ifend program prime```5. 习题五:编写一个Fortran程序,计算并输出一个给定整数的所有因子。

fortran作业

fortran作业

fortran作业第三章二、将数学式写成fortran表达式(1)a**2+4*b**3)/(a-b)(2)(-b+sqrt(b*b-4*a*c))/(2*a)(3)(6*sin((x+y)**2))/(2*a)(4)sin(y/(aqrt(x*x+y*y)))(5)sin(atan(aqrt(x*x+y*y))/(abs(c)))九、电路编程implicitreal(a-z)r1=30r2=60r3=45u=120i=(u/r1+u/r2+u/r3)print*,iend结果:8.666667十、六边形面积编程!mainprogramimplicitreal(a-z)i1=10i2=20i3=16i4=13i5=21i6=14i7=30i8=36i9=28area=x(i2,i3,i7)+x(i1,i7,i8)+x(i4,i8,i9)+x(i5,i6,i9)print*,'area=',areaend!functionprogramfunctionx(a1,a2,a3)b=(a1+a2+a3)/2x=sqrt(b*(b-a1)*(b-a2)*(b-a3))end结果:area=581.2570十一、分期付款编程read(*,*)d,p,rm=(log(p)-log(p-d*r))/log(1+r)m=m+1print*,mend十二、筹钱贷款编程!daikuanrealiread(*,*)a,r,ni=r+1d=(a*(i**n-1))/((i-1)*i**n)d=int(d*10+0.5)/10print*,dend第四章五、谋y编程!calculateyrealx,yread(*,*)xif(x.ge.0.and.x.lt.10)theny=xelseif(x.ge.10.and. x.lt.20)theny=x*x+1elseif(x.ge.20.and.x.lt.30)theny=x*x*x+x*x+1endifprint*,'x=',x,'y=',yend六、整除编程!zhengchuread(*,*)mif(mod(m,7).eq.0)print*,m,'能被7整除'if(mod(m,11).eq.0)print*,m,'能被11整除'if(mod(m,17).eq.0)print*,m,'能够被17相乘'if(mod(m,7).ne.0.and.mod(m,11).ne.0.and.mod(m,17).ne.0)print*,m,'无法被7,11,17相乘'end八、大小排序编程!programread(*,*)a,b,c,dif(a.lt.b)thentemp=aa=bb=tempendifif(a.lt.c)thentemp=aa=cc=tem pendifif(a.lt.d)thentemp=aa=dd=tempendifif(b.lt.c)thentemp=bb=cc=tempendifif(b.lt.d)thentemp=bb=dd=tempendifif(c.lt.d) thentemp=cc=dd=tempendifprint*,a,b,c,dend九、高程编程!programread(*,*)x,yif((x-2)**2+(y-2)**2.le.1)thenh=10elseif((x+2)**2+(y+2)**2.le.1)thenh=10elseif((x-2)**2+(y+2)**2.le.1)thenh=10elseif((x+2)**2+(y-2)**2.le.1)thenh=10elseh=0endifprint*,'h=',hend十、建筑规划编程!programread(*,*)x,yif(abs(x).le.10.and.abs(y).le.10)thenh=20elseif(abs(x).l e.20.and.abs(y).le.20)thenh=30elseif(abs(x).le.30.and.abs(y).le.30)thenh=50elseh=100endifprint*,'h=',hend一、求和编程!programsum=0sign=1do100,i=1,100,1sum=sum+sign*1.0/isign=sign*(-1)100continueprint*,'sum=',sumend结果为:0.6881718二、求和编程!programsum=0do100,i=1,20,1第五章sum=sum+1.0/i/(i+1)100continueprint*,'sum=',sumend结果为:0.9900995四、sinx函数编程!programsum=0read(*,*)xtemp=xsign=1i=1do100,while(abs(temp).gt.1e-10)sum=sum+sign*tempsign=sign*(-1)i=i+2temp=temp*x*x/i/(i-1)100continueprint*,sumend五、电阻编程!programread(*,*)r0,r1,r2temp=r0do100,i=1,3,1temp=(temp+r1)*r2/(temp+r1+r2)1 00continueprint*,tempend七、水解因子编程!programread(*,*)mprint*,m,'=1'20continuedo100,i=2,m,1if(mod(m,i).eq.0)thenprint*,'*',im=m/igoto20endif100continue。

fortran程序案例题汇编(14道)-推荐下载

fortran程序案例题汇编(14道)-推荐下载
6.用函数子程序的方法设计一个判断某个数是否是素数的程序,统计 100~1000 内的素数 的个数。 logical function prime(n) logical t t=.true. do i=2,n-1 if(mod(n,i)==0)then
t=.false. exit endif enddo prime=t end
1
对全部高中资料试卷电气设备,在安装过程中以及安装结束后进行高中资料试卷调整试验;通电检查所有设备高中资料电试力卷保相护互装作置用调与试相技互术关,系电通,力1根保过据护管生高线产中0不工资仅艺料可高试以中卷解资配决料置吊试技顶卷术层要是配求指置,机不对组规电在范气进高设行中备继资进电料行保试空护卷载高问与中题带资2负料2,荷试而下卷且高总可中体保资配障料置2试时32卷,3各调需类控要管试在路验最习;大题对限到设度位备内。进来在行确管调保路整机敷使组设其高过在中程正资1常料中工试,况卷要下安加与全强过,看度并25工且52作尽22下可护都能1关可地于以缩管正小路常故高工障中作高资;中料对资试于料卷继试连电卷接保破管护坏口进范处行围理整,高核或中对者资定对料值某试,些卷审异弯核常扁与高度校中固对资定图料盒纸试位,卷置编工.写况保复进护杂行层设自防备动腐与处跨装理接置,地高尤线中其弯资要曲料避半试免径卷错标调误高试高等方中,案资要,料求编试技5写、卷术重电保交要气护底设设装。备备置管4高调、动线中试电作敷资高气,设料中课并技3试资件且、术卷料中拒管试试调绝路包验卷试动敷含方技作设线案术,技槽以来术、及避管系免架统不等启必多动要项方高方案中式;资,对料为整试解套卷决启突高动然中过停语程机文中。电高因气中此课资,件料电中试力管卷高壁电中薄气资、设料接备试口进卷不行保严调护等试装问工置题作调,并试合且技理进术利行,用过要管关求线运电敷行力设高保技中护术资装。料置线试做缆卷到敷技准设术确原指灵则导活:。。在对对分于于线调差盒试动处过保,程护当中装不高置同中高电资中压料资回试料路卷试交技卷叉术调时问试,题技应,术采作是用为指金调发属试电隔人机板员一进,变行需压隔要器开在组处事在理前发;掌生同握内一图部线纸故槽资障内料时,、,强设需电备要回制进路造行须厂外同家部时出电切具源断高高习中中题资资电料料源试试,卷卷线试切缆验除敷报从设告而完与采毕相用,关高要技中进术资行资料检料试查,卷和并主检且要测了保处解护理现装。场置设。备高中资料试卷布置情况与有关高中资料试卷电气系统接线等情况,然后根据规范与规程规定,制定设备调试高中资料试卷方案。

(完整word版)fortran程序实例

(完整word版)fortran程序实例

1) 实例3 —求多个半径下的圆周长 ! z3.f90 --Fortran95 FUNCTIONS:-Entry point of con sole application.I*************************************************************************PROGRAM: z3PURPOSE: En try point for the con sole applicati on.I************************************************************************program z3 !求多个半径下的圆周长 !主程序! PROGRAM Z3PRINT *, 'R=',1.2,'C=',C(1.2) PRINT *, 'R=',3.4,'C=',C(3.4) PRINT *, 'R=',15.6,'C=',C(15.6) PRINT *, 'R=',567.3,'C=',C(567.3) END program z3 !子程序FUNCTION C(R) PI=3.1415926 C=2*PI*R RETURN ! Body of z3 endz3•■Alta• JSwFta.■■ TTjn* FMIIH-.T jd - |叭■(Lld4ilh■ Hi li tii.IW M H MvfriiMfeai Iwi Ji JJII+M4Tml+n««I*界rFHtlE- HfrtpliK * W mita itflLuLfl-PRINT* ,'以上为计算机的计算结果,注意 B 的值'ZXZ I O.f90 FUNCTIONS: ZXZ_I_O- Entry point of con sole applicati on.PROGRAM: ZXZ_I_OPURPOSE: En try point for the con sole applicati on.program ZXZ_I_O implicit none!变量声明的位置INTEGER(2) i; INTEGER(4) j; INTEGER(4) m; REAL n INTEGER A,B PRINT*,'输入整数 A'; READ*, A PRINT*,'输入整数 B'; READ*, B B=A+BWRITE(*,*) 'A*B=',A*B实例4 —键盘与显示器输入/输出2)a) Fortra n 基本操作1 B Li PlLu- i \ JfiL ■ I■' hi -IJBL . n :»'匹:"b )程序指令BIE1A J~. C ■* «I*************** 输入、输出样式种种 **************************!系统默认的输出样式 PRINT* ,'系统默认的输出样式' !人为控制的的输出样式--格式化输出 i=21; j=53; m=5 n=(i+j*m*i**m) WRITE(*,*) 'i,j,m 是常量,程序赋初值PRINT*, i,j,m WRITE(*,*) 'i,j,m 的计算结果:'PRINT*,'i+j*m*i**m=' ,nPRINT* ,''! Body of ZXZ_I_O end program ZXZ_I_O 程序说明: 程序赋值一初始化 i=21; j=53; m=5 键盘无格式输入 READ*, A 键盘有格式输入 READ ( *, 100)A,B,C 100 FORMA T( 2F5.2,F5.3) 显示器无格式输出PRINT* ,'系统默认的输出样式' WRITE(*,*) 'A*B=',A*B 显示器有格式输出 PRINT 100 ,A+B WRITE(*,100) 'A*B=',A*B 100 FORMA T( F5.2)C )调试运行d )程序指令pruqrdiri ZX2_l_0 iflnpllclt iioiiv 陝量声明的立置IHTEGERfZ) i; IMTEGEimi ]; IHTEGER(^)叭 REAL nA.B I Uairidbles■HIKE J 播入整該■打1E.D 屯A PRINT •,-KM«a■ e-fltBFHJHIv T -H-Ai-B-1 ,ti' Axb- 1 T A I UHU NA 「臥存为计負机药计貝韭呆*妊憲L1的低・1-21; j-b3;«R[TEC-,«) l,j,n 是常最.程序Itt 前值・FHIHTa, 1 J ,■WR1TF(-F -J l p j,n 的tt 幣结黑,.FHim* /■r u 讪o#畑」」Ffirl prnni'an ZK? I o! ZXZ丄O.f90! FUNCTIONS:ZXZ_I_O - Entry point of con sole applicati on.PROGRAM: ZXZ_I_OPURPOSE: En try point for the con sole applicati on.1 *************** 输^入 ^输出样式种不中program ZXZ_I_Oimplicit none!变量声明的位置INTEGER(2) i; INTEGER(4) j; INTEGER(4) m; REAL nINTEGER A,BREAL X,Y ,Z ! VariablesPRINT*,'输入整数A'; READ*, APRINT*,'输入整数B'; READ*, B PRINT*,'计算结果为:'B=A+BPRINT*,'B=A+B=',BWRITE(*,*) 'A*B=',A*BPRINT* ,'以上为计算机的计算结果,注意B的值'!系统默认的输出样式PRINT* ,'系统默认的输出样式'PRINT*,'输入实数X'; READ(*,100) XPRINT*,'输入实数Y:READ(*,100) Y100 FORMA T(F5.2)PRINT*,'计算结果为:'Z=X+YPRINT 200,Z200 FORMA T(4X,'Z=X+Y=',F8.3)WRITE(*,*)WRITE(*,300) X*Y300 FORMA T(4X,'Z=X*Y=',F8.3)!人为控制的的输出样式--格式化输出PRINT* ,'程序为常量赋了初值'i=21; j=53; m=5n=(i+j*m*i**m)WRITE(*,*) 'i,j,m 是常量,程序赋初值PRINT*, i,j,mWRITE(*,*) 'i,j,m 的计算结果:'PRINT*,'i+j*m*i**m=' ,nPRINT* ,' '! Body of ZXZ_I_Oend program ZXZ_I_Oe)调试运行rRIHT* JPRTNi*. ■讦薛结果为.・ E^A*BPRTNT*,,R=A+R=,,8 URrfEfw,*) 'ft-D-1t A-DPRINT * J 以上药i1■宴利的iT 畀结果.傢金默认囱馥出择畫PRINT*「系唏默认册葆出样式・ 阳IHH. •揃扎斓 W : WIHZJ 输入宪数V;1B0 F0RHAKF5.2)PRINT -,■廿尊结杲为FREAD(*,1ua ) K R 匚flD(*,100) VPRINT 2Q0左2(JU FUKMfil(4i(t ; = «+/= \FB »J) unrrEf*.*)WRITE(*r 30O) X*V FORMfiT(UX /? = X*V=* ,F8 .3}认为扌前函的输世#戎一逼占化输出PRTHT- ”囉序対常豊瓯了初值・ i^Z» i j=53;I1=5 n=(i +j*m*i**n)WRITEf"^) 'i t j t n 是常量,程序赋初值 PRTHTw, i r j F nMRlTEe 』)・i J,n 的计具结衆:,PRI HI*.'i+j *R*i**m=',n。

Fortran练习经典题目

Fortran练习经典题目

Fortran练习经典题目a, 输入一个年、月、日并计算它是本年度的第几天。

b.输入任意一年份,给出该年出生人的属相,如1945年出生的人的属相为“鸡”。

c.显示输出2000~2099年的任何一年的某一月的月历,所要显示的年月有键盘输入。

如2002年5月的月历形式如下;5月2002年日一二三四五六1 2 3 4 5 6 78 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30 31d.显示输出2000~2099年的任何一年的年历,并写入一文本文件中。

e.以上功能都应从菜单中选择执行。

提示:a中若y为年份,d是该年的某月某日从1月1日开始经过的天数,则s=(y-1)*1.2425+d。

若s除以7后所得的余数取整后为0,则为周日,为1则周一。

program firstquestionimplicit noneinteger::q,i,ldayinteger::year,month,day,days,mday,dscharacter(len=2),dimension(1:37)::tem! 这个程序可以同时在屏幕和文本中输出计算结果。

为了计算不同的问题,引入变量q,当q=1时,计算某年月日是此年的第几天;! 当q=2时,计算某年的属相;当q=3时,计算月历;当q=4时,计算年历;当q=0时,退出此程序。

此外,此程序还设置了对输入! 年、月、日的判断,判断其是否合法。

print *,'when q=1,this program can resolve the question thatwhich day some day is in some year! this is the question one.' print *,'when q=2,this program can obtain the shengxiao of some year! this is the question two.'print *,'when q=3,this program can obtain the calendar of some month of some year(2000-2099)! this is the question three.' print *,'when q=4,this program can obtain the Almanac of some year(2000-2099)! this is the question four.'print *,'If you want to exit from this program,please input q=0.'doprint *,'input the number of q:'read *,qif(q==0) exitif (q==1) then2000 print *,'input the year, the month and the day:'read *,year,month,dayif (month>12.or.month<0) print *,'you input wrong month.' if (month>12.or.month<0) goto 2000call leapday(year,month,day,lday)if (lday==0) print *,'you input wrong day.'if (lday==0) goto 2000call numday(year,month,day,days)print '(a14,i3,a18)','The day is the',days,' day of this year.'open(25,file='days.dat',status='unknown')write(25,'(a14,i3,a18)') 'The day is the',days,' day of this year.' close(25)else if (q==2) thenprint *,'input the year:'read *,yearcall shengxiao(year)else if (q==3) then2001 print *,'input the year(2000-2099) and the month:'read *,year,monthif (year>2099.or.year<2000) print *,'you input wrong year,please input the year between 2000 and 2099.'if (year>2099.or.year<2000) goto 2001if (month>12.or.month<0) print *,'you input wrong month.' if (month>12.or.month<0) goto 2001open(27,file='yueli.dat',status='unknown')call nyli(year,month,mday,ds,tem)write(27, "(26x,i2,'月',3x,i4,'年')")month,yearwrite(27, '(18x,7a4)')'日','一','二','三','四','五','六'write(27, '(18x,7a4)')(tem(i),i=1,mday+ds)write(27,*)close(27)else if (q==4) then2002 print *,'input the year(2000-2099):'read *,yearif (year>2099.or.year<2000) print *,'you input wrong year,pleade input the year between 2000 and 2099.' if (year>2099.or.year<2000) goto 2002open(28,file='nianli.dat',status='unknown')do month=1,12call nyli(year,month,mday,ds,tem)write(28, "(26x,i2,'月',3x,i4,'年')")month,yearwrite(28, '(18x,7a4)')'日','一','二','三','四','五','六'write(28, '(18x,7a4)')(tem(i),i=1,mday+ds)write(28,*)end doclose(28)elseprint *,'you input the wrong number of q.' end ifend doend program firstquestionsubroutine shengxiao(year)implicit noneinteger::year,m,nm=year-1945if (m>=12.or.m<=-12) thenn=mod(m,12)elsen=mend ifopen(26,file='shengxiao.dat',status='unknown') if (n==0) thenprint *,'The shengxiao of this year is:鸡'write(26,*)'The shengxiao of this year is:鸡' else if(n==1.or.n==-11) thenprint *,'The shengxiao of this year is:狗'write(26,*)'The shengxiao of this year is:狗' else if(n==2.or.n==-10) thenprint *,'The shengxiao of this year is:猪'write(26,*)'The shengxiao of this year is:猪' else if(n==3.or.n==-9) thenprint *,'The shengxiao of this year is:鼠'write(26,*)'The shengxiao of this year is:鼠' else if(n==4.or.n==-8) thenprint *,'The shengxiao of this year is:牛'write(26,*)'The shengxiao of this year is:牛'else if(n==5.or.n==-7) thenprint *,'The shengxiao of this year is:虎'write(26,*)'The shengxiao of this year is:虎' else if(n==6.or.n==-6) thenprint *,'The shengxiao of this year is:兔'write(26,*)'The shengxiao of this year is:兔' else if(n==7.or.n==-5) thenprint *,'The shengxiao of this year is:尨'write(26,*)'The shengxiao of this year is:尨' else if(n==8.or.n==-4) thenprint *,'The shengxiao of this year is:蛇'write(26,*)'The shengxiao of this year is:蛇' else if(n==9.or.n==-3) thenprint *,'The shengxiao of this year is:马'write(26,*)'The shengxiao of this year is:马' else if(n==10.or.n==-2) thenprint *,'The shengxiao of this year is:羊'write(26,*)'The shengxiao of this year is:羊' else if(n==11.or.n==-1) thenprint *,'The shengxiao of this year is:猴'write(26,*)'The shengxiao of this year is:猴' end ifclose(26)end subroutine shengxiaosubroutine nyli(year,month,mday,ds,tem) implicit noneinteger::year,month,days,mday integer::i,m,ds,dts,qreal::scharacter(len=2),dimension(1:37)::temcall numday(year,month,1,days)call monday(year,month,mday)s=(year-1)*1.2425+daysds=int(s-int(s/7)*7)do i=1,37tem(ds+1)='1';tem(ds+11)='11';tem(ds+12)='12';tem(ds+2)='2';tem(ds+13)='13';tem(ds+14)='14';tem(ds+3)='3';tem(ds+15)='15';tem(ds+16)='16';tem(ds+4)='4';tem(ds+17)='17';tem(ds+18)='18';tem(ds+5)='5';tem(ds+19)='19';tem(ds+20)='20';tem(ds+6)='6';tem(ds+21)='21';tem(ds+22)='22';tem(ds+7)='7';tem(ds+23)='23';tem(ds+24)='24';tem(ds+8)='8';tem(ds+25)='25';tem(ds+26)='26';tem(ds+9)='9';tem(ds+27)='27';tem(ds+28)='28';tem(ds+10)='10';tem(ds+29)='29';tem(ds+30)='30';tem(ds+ 31)='31'; print "(26x,i2,'月',3x,i4,'年')",month,yearprint '(18x,7a4)','日','一','二','三','四','五','六'print '(18x,7a4)',(tem(i),i=1,mday+ds)print *end subroutine nylisubroutine numday(year,month,day,days)implicit noneinteger::year,month,day,days,nlogical::leapcall leapyear(year,leap)n=int(month/2)if (month<=8) thenif (mod(month,2)/=0) thendays=30*n+31*n+dayelsedays=30*(n-1)+31*n+dayend ifelseif (mod(month,2)/=0) thendays=30*(n-1)+31*(n+1)+dayelsedays=30*(n-1)+31*n+dayif(month>2) thenif (leap) thendays=days-1elsedays=days-2end ifend ifend subroutine numdaysubroutine monday(year,month,mday) implicit none integer::year,month,mday logical::leapcall leapyear(year,leap)select case (month)case (4,6,9,11)mday=30case (1,3,5,7,8,10,12)mday=31case (2)for_feb:select case (leap)case (.true.)mday=29case (.false.)mday=28end select for_febcase defaultend selectend subroutine monday subroutine leapyear(year,leap)implicit noneinteger::yearlogical::leapif (mod(year,4)/=0) thenleap=.false.else if (mod(year,100)/=0) thenleap=.true.else if (mod(year,400)/=0) thenleap=.true.elseleap=.false.end ifend subroutine leapyearsubroutine leapday(year,month,day,lday) implicit none integer::year,month,day,lday logical::leapcall leapyear(year,leap)select case (month)case (4,6,9,11)if (day>30.or.day<1) thenlday=0elselday=1end ifcase (1,3,5,7,8,10,12)if (day>31.or.day<1) thenlday=0elselday=1end ifcase (2)for_feb:select case (leap)case (.true.)if (day>29.or.day<1) thenlday=0elselday=1end ifcase (.false.)if (day>28.or.day<1) thenlday=0elselday=1end ifend select for_febcase defaultend selectend subroutine leapday素数是只能被1与其本身整除的数,下面给出一种寻找素数的方法:a生存一个数组,将其所有的元素初始化为1b 从数组下标为2的元素开始,每次寻找数值为1 的数组元素。

fortran 编程练习题

fortran 编程练习题
if(a(s)<=b(k).and.s<=m)then c(i)=a(s) s=s+1 else if(a(s)>b(k).and.k<=n)then c(i)=b(k) k=k+1 else if(s>m)then c(i)=b(k) k=k+1 else if(k>n)then c(i)=a(s) s=s+1 end if end if end if end if end do write(*,*)"排列整合后的数列为:" write(*,"(1x,7f10.4)")(c(i),i=1,m+n) end
program main implicit none real a(1:25),b(1:25),c(1:50) integer i,s,k,m,n write(*,*)"输入数组a元素个数m=" read(*,*)m write(*,*)"输入数组a各元素:" read(*,*)(a(i),i=1,m) write(*,*)"输入数组b元素个数n=" read(*,*)n write(*,*)"输入数组b各元素:" read(*,*)(b(i),i=1,n) s=1 k=1 do i=1,m+n
3.输出所有水仙花数(水仙花数是指一个三位正整数,其各位数字的 立方和等于该数本身)。
program main implicit none integer a,b,c,d do 5 d=100,999 a=mod(d/100,10) b=mod(d/10,10)
c=mod(d,10) if (d==a**3+b**3+c**3) then write(*,15) 15 format(1x,i3,"为a水仙花数" ) else write(*,25) 25 format(1x,i3,"不为水仙花数" ) endif 5 d=d+1 stop end

FORTRAN练习题

FORTRAN练习题

FORTRAN练习题1.当执行下列语句时在终端上键入123456.789,问该语句执行后X,Y,Z的值是什么?READ (*,100)X,Y,Z100 FORMA T(3F4.2)2.分别写出由下列三个循环语句所确定的循环次数100DO 10 I=3,6,3100 DO 10 K=6,3,3100 DO 10 X=-0.5,-4.5,-0.53.已知I=5,J=2,A=5.0,B=2.0,问执行下列程序段后,M,N,X,Y,的值分别是什么?M=I/JN=A/BX=I/JY=A/B4.写出下列语句段输出的内容A=-1.2B=7.36WRITE(*,100)A,B,A+B100 FORMA T (1X,2F7.1/1X,’A+B=’,F7.1)5.已知X=5.7,Y=-123.0,Z=12.796,试写出下列语句的输出结果。

WRITE (*,100) X,Y,Z100 FORMA T (1X,2F6.2)6.下列主程序是否正确?不正确则指出其错误READ (*,*)NDIMENSION A(N)READ(*,*) (A(I),I=1,N)S=0.0DO 10 I=1,N10S=S+A(I)WRITE(*,*)SEND7.阅读程序,回答问题DIMENSION M(3,3)DATA M/1,2,3,4,5,6,7,8,9/40DO 10 I=1,250DO 10 J=I+1,3K=M(I,J)M(I,J)=M(J,I)10M(J,I)=KWRITE(*,100)((M(I,J),J=1,3),I=1,3)100FORMA T(1X,3I2)END问题1:经DA TA语句赋值后,数组M的第一行元素的值是()问题2:程序执行后输出的第一行结果是()问题3:若将标号为40,50的语句改为40DO 10 I=1,350DO 10 J=1,3程序执行后输出的第一行结果是()8.阅读程序,回答问题READ (*,*) M,NIF (M .LT. N) THENK=MM=NN=KENDIFL=MOD(M,N)10IF (L .NE. 0) THENM=NN=LL=MOD(M,N)GOTO 10ENDIFWRITE(*,*) NEND问题1:程序执行时,若M,N的输入值为9,15,则输出的N值为()。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.Fibonacci数列定义如下:F1=1F2=1F n=F n-1+F n-2 (n>2)求Fibonacci数列的前30项。

integer f(30),if(1)=1f(2)=2do i=3,30f(i)=f(i-1)+f(i-2)enddoprint*,fend2.输入10个学生的总分,求每个学生的名次integer s(10),a(10),i,jdo i=1,10read*,s(i)enddodo i=1,10a(i)=1do j=1,10if(s(i)<s(j)) a(i)=a(i)+1enddoenddodo i=1,10print*,s(i),a(i)enddoend3.给定一组数,按照从小到大的顺序输出。

integer a(10)integer pdo i=1,10read *,a(i)enddodo j=1,9p=jdo i=j+1,10if (a(i)<a(p)) p=ienddoif (p/=j) thent=a(p);a(p)=a(j);a(j)=tendifenddoprint *,(a(i),i=1,10)end4.输入若干名学生的学号和三门课程(语数英)的成绩,要求从键盘输入一个学生的学号,能打印出该学生的三门功课成绩和总分。

character*6,dimension(:),allocatable::xueinteger,dimension(:,:),allocatable::ginteger,dimension(:),allocatable::zonginteger i,j,ncharacter*6,xhprint *,"请输入学生的个数"read *,nallocate(xue(n))allocate(g(n,3))allocate(zong(n))do i=1,nread *,xue(i),(g(i,j),j=1,3)enddodo i=1,nzong(i)=0do j=1,3zong(i)=zong(i)+g(i,j)enddoenddoprint *,"请输入你要打印的学生的学号"read *,xhdo i=1,nif(xue(i)==xh)thenprint *,(g(i,j),j=1,3),zong(i)exitendifenddoend5.编写一个函数子程序计算所输入两个整数m、n的最大公约数。

integer function gcd(m,n)integer rr=mod(m,n)do while (r/=0)m=nn=rr=mod(m,n)enddogcd=nendinteger x,y,gcdread*,x,yprint*,gcd(x,y)end6.用函数子程序的方法设计一个判断某个数是否是素数的程序,统计100~1000内的素数的个数。

logical function prime(n)logical tt=.true.do i=2,n-1if(mod(n,i)==0)thent=.false.exitendifenddoprime=tendinteger slogical primes=0do i=100,1000if(prime(i))thens=s+1endifenddoprint*,send7.设计一个子程序,对于自然数m,n,该函数求m,n的最小公倍数。

integer function gcd(m,n)integer r,tt=m*nr=mod(m,n)do while (r/=0)m=nn=rr=mod(m,n)enddogcd=t/nendinteger x,y,gcdprint*,"请输入两个数"read*,x,yprint*,gcd(x,y)end8.对任意自然数n,设计一个求n的各位数字的立方和的子程序,并调用该子程序求100~999之间的所有水仙花数。

subroutine shu(n,t)integer n,i,a,b,clogical ta=n/100b=mod(n/10,10)c=mod(n,10)if(a**3+b**3+c**3==n)thent=.true.elset=.false.endifendlogical tdo i=100,999call shu(i,t)if(t)thenprint*,iendifenddoend9.设计一个子例行程序SORT(A,N,K),其中A是一个一维数组,N是A的元素个数,SORT 的功能是:当K=1时,将数组A按升序排列;当K=0时,将数组A按降序排列;当K为其它数值时,数组A保持原序。

调用该子程序根据不同输入得出数组不同的输出方式。

subroutine sort(a,n,k)integer a(n)integer k,iselect case(k)case (0)do i=1,n-1do j=1,n-Iif(a(j)>a(j+1)) thent=a(j)a(j)=a(j+1)a(j+1)=tendifenddoenddocase(1)do i=1,n-1do j=1,n-Iif(a(j)<a(j+1)) thent=a(j)a(j)=a(j+1)a(j+1)=tendifenddoenddoend selectendreal,dimension(:),allocatable::binteger k,nprint*,"请输入要排序的数据数目"read*,nallocate(b(n))print *,"请输入这n个数据"read *,(b(i),i=1,n)print*,"请输入k的值"read *,kcall sort(b,n,k)print *,(b(i),i=1,n)end10.编写一个判断闰年的程序。

调用该过程输出2011~2050之间所有的闰年。

function s(n)integer nlogical sif(mod(n,4)==0.and.mod(n,100)/=0.or.mod(n,400)==0)thens=.true.elses=.false.endifendlogical sdo i=2011,2050if(s(i))thenprint*,iendifenddoend11.利用子程序机制实现:输入若干名学生的学号和三门课程的成绩;计算每个学生的总分;输出每个学生的学号、三门课程的成绩和总分。

integer,dimension(:,:),allocatable::gradeinteger,dimension(:),allocatable::zonginteger,dimension(:),allocatable::mingciinteger m,nread *,m,nallocate (grade(m,n))allocate(zong(m))allocate(mingci(m))call input(grade,m,n)call sum1(grade,m,n,zong)call qiuming(zong,m,mingci)call output(grade,m,n,zong,mingci)endsubroutine input(grade,m,n)integer grade(m,n)print *,"请输入成绩"do i=1,mread *,(grade(i,j),j=1,n)enddoendsubroutine sum1(grade,m,n,zong)integer grade(m,n)integer zong(m)zong=0do i=1,mdo j=1,nzong(i)=zong(i)+grade(i,j)enddoenddoendsubroutine qiuming(zong,m,mingci)integer zong(m)integer mingci(m)mingci=1do i=1,mdo j=1,mif(zong(i)<zong(j))thenmingci(i)=mingci(i)+1endifenddoenddoendsubroutine output(grade,m,n,zong,mingci)integer grade(m,n)integer zong(m)integer mingci(m)do i=1,mprint *,(grade(i,j),j=1,n) ,zong(i),mingci(i)enddoend12.利用递归函数方法实现:求n !;然后调用该函数求integer recursive function fac(n) result(f)if(n==1) thenf=1elsef=n*fac(n-1)endifendparameter(pi=3.1416926)real x,sinteger n,fs=1print*,"请输入角度值"read*,xx=x*pi/180f=-1n=2do while(abs(f*x**n/fac(n)>1e-6)s=s+f*x**n/fac(n)n=n+2f=-fenddoprint*,s end.10...!6!4!21)cos(6642-<+-+-=直到最后一项x x x x13.输入10名学生的学号、姓名、性别和一门课程的成绩,要求打印出不及格学生的所有信息。

type scharacter*11 numcharacter*8 namelogical sexreal gradeend typetype(s) g(10)integer i,jprint*,"请输入学生的基本信息"do i=1,10read*,g(i).num,g(i).name,g(i).sex,g(i).gradeenddodo i=1,10if(g(i).grade<60)thenj=iendifenddoprint*,g(j).num,g(j).name,g(j).sex,g(j).gradeend14.已知职工工资表记录包括:职工号,姓名,年龄、职称、工资,建立一个10个职工组成的记录表,打印输出职工中工资最高者和最低者所有信息,以及工资总额和平均工资。

type empcharacter*6 numcharacter*8 nameinteger agecharacter*16 zhichenreal wageendtypetype(emp) s(10)integer ireal max,min,sum,avgprint*,"请输入职工的基本信息"do i=1,10read *,s(i).num,s(i).name,s(i).age,s(i).zhichen,s(i).wageenddomax=s(1).wage;j=1do i=2,10if(max<s(i).wage)thenmax=s(i).wagej=iendifenddomax=s(1).wage;k=1do i=2,10if(min>s(i).wage)thenmin=s(i).wagek=iendifenddosum=0do i=1,10sum=sum+s(i).wageenddoavg=sum/10print *,"输出最高工资的职工信息"print *,s(j).num,s(j).name,s(j).age,s(j).zhichen,s(j).wageprint *,"输出最低工资的职工信息"print *,s(k).num,s(k).name,s(k).age,s(k).zhichen,s(k).wageprint *,"输出工资总额和平均值"print *,sum,avgend11。

相关文档
最新文档