fortran程序案例题汇编(14道)
- 格式:docx
- 大小:22.78 KB
- 文档页数:11
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编程习题的答案,帮助您更好地理解和掌握这门语言。
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表达式(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。
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练习经典题目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练习题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:编程找出并输出100—150之间和400—450之间能被9整除的数。
程序:program ex0401implicit noneinteger ido i=100, 150if(mod(i,9).eq.0) thenwrite(*,10) i10 format(i3)end ifend dodo i=400, 450if(mod(i,9).eq.0) thenwrite(*,20) i20 format(i3)end ifend doend运行结果:2:回文数是指正读与反读都一样的数。
如:232,编程求100到999之间的回文数。
程序:program ex0402implicit noneinteger ainteger cinteger ido 10 i=100,999a=i/100 !百位上的数字c=mod(i,10) !个位上的数字if(a.eq.c) thenwrite(*,20) i20 format(i8)endif10 continueEnd运行结果:3:输出所有水仙花数(水仙花数是指一个三位正整数,其各位数字的立方和等于该数本身)编程:program ex0403implicit noneinteger ainteger binteger cinteger iwrite(*,*)'水仙花数:'do 10 i=100,999a=i/100 !百位上的数字b=mod(i,100)/10 !十位上的数字c=mod(i,10) !个位上的数字if(i.eq.a**3+b**3+c**3) thenwrite(*,20)i,a,b,c20 format(5x,i3,'=',i2,'^3+',i2,'^3+',i2,'^3')endif10 continueend运行结果:4:编程输出如下图形** * ** * * * ** * * * * * ** * * * ** * **编程:program ex0404implicit noneinteger i,jcharacter a(7)do i=1,7 !输入数组a(i)='*'end dodo j=1,4 !前四行write(*,'(<3*(4-j)>x,10a3)')(a(i),i=1,2*j-1)end dodo j=3,1,-1 !后三行write(*,'(<3*(4-j)>x,10a3)')(a(i),i=1,2*j-1)end doend运行结果:5:输入n个数,找出所有大于n个数的平均值的那些数及最小数。
题目:《FORTRAN语言》算例程序专业:班级:学号:姓名:时间:目录1.平面深梁有限元前处理信息生成1.1深梁有限元模型(3)1.2源程序(3)1.3输出结果和分析(4)2.矩阵求逆子程序调试2.1子程序源程序(9)2.2考题(11)2.2.1主程序(11)2.3考题及验证(11)1.平面深梁有限元前处理信息生成1.1深梁有限元模型16117176 81 每个小方格长和宽都为0.25。
1.2源程序c programdimension xy(85,2),ijk(128,3)open(1,file="xyijk.dat",status="unknown")do i=1,17do j=1,5m=(i-1)*5+jxy(m,1)=0.25*(i-1)xy(m,2)=0.25*(j-1)end doend dodo i=1,85write(1,*) i, (xy(i,j),j=1,2)end dodo i=1,16do j=1,4ns=8*(i-1)+jnx=ns+4ijk(ns,1)=(i-1)*5+jijk(ns,2)=ijk(ns,1)+6ijk(ns,3)=ijk(ns,1)+1ijk(nx,1)=ijk(ns,1)ijk(nx,2)=ijk(ns,1)+5ijk(nx,3)=ijk(ns,1)+6end doend dodo i=1,128write(1,*) i, (ijk(i,j),j=1,3)end doclose(1)end1.3输出结果及分析1 0.000000E+00 0.000000E+002 0.000000E+00 2.500000E-013 0.000000E+00 5.000000E-014 0.000000E+00 7.500000E-015 0.000000E+00 1.0000006 2.500000E-01 0.000000E+007 2.500000E-01 2.500000E-018 2.500000E-01 5.000000E-019 2.500000E-01 7.500000E-0110 2.500000E-01 1.00000011 5.000000E-01 0.000000E+0012 5.000000E-01 2.500000E-0113 5.000000E-01 5.000000E-0114 5.000000E-01 7.500000E-0115 5.000000E-01 1.00000016 7.500000E-01 0.000000E+0017 7.500000E-01 2.500000E-0118 7.500000E-01 5.000000E-0119 7.500000E-01 7.500000E-0121 1.000000 0.000000E+0022 1.000000 2.500000E-0123 1.000000 5.000000E-0124 1.000000 7.500000E-0125 1.000000 1.00000026 1.250000 0.000000E+0027 1.250000 2.500000E-0128 1.250000 5.000000E-0129 1.250000 7.500000E-0130 1.250000 1.00000031 1.500000 0.000000E+0032 1.500000 2.500000E-0133 1.500000 5.000000E-0134 1.500000 7.500000E-0135 1.500000 1.00000036 1.750000 0.000000E+0037 1.750000 2.500000E-0138 1.750000 5.000000E-0139 1.750000 7.500000E-0140 1.750000 1.00000041 2.000000 0.000000E+0042 2.000000 2.500000E-0143 2.000000 5.000000E-0144 2.000000 7.500000E-0145 2.000000 1.00000046 2.250000 0.000000E+0047 2.250000 2.500000E-0148 2.250000 5.000000E-0149 2.250000 7.500000E-0150 2.250000 1.00000051 2.500000 0.000000E+0052 2.500000 2.500000E-0153 2.500000 5.000000E-0154 2.500000 7.500000E-0155 2.500000 1.00000056 2.750000 0.000000E+0057 2.750000 2.500000E-0158 2.750000 5.000000E-0159 2.750000 7.500000E-0160 2.750000 1.00000061 3.000000 0.000000E+0062 3.000000 2.500000E-0163 3.000000 5.000000E-0165 3.000000 1.00000066 3.250000 0.000000E+0067 3.250000 2.500000E-0168 3.250000 5.000000E-0169 3.250000 7.500000E-0170 3.250000 1.00000071 3.500000 0.000000E+0072 3.500000 2.500000E-0173 3.500000 5.000000E-0174 3.500000 7.500000E-0175 3.500000 1.00000076 3.750000 0.000000E+0077 3.750000 2.500000E-0178 3.750000 5.000000E-0179 3.750000 7.500000E-0180 3.750000 1.00000081 4.000000 0.000000E+0082 4.000000 2.500000E-0183 4.000000 5.000000E-0184 4.000000 7.500000E-0185 4.000000 1.0000001 1 7 22 2 8 33 3 9 44 4 10 55 16 76 27 87 3 8 98 4 9 109 6 12 710 7 13 811 8 14 912 9 15 1013 6 11 1214 7 12 1315 8 13 1416 9 14 1517 11 17 1218 12 18 1319 13 19 1420 14 20 1521 11 16 1722 12 17 1824 14 19 2025 16 22 1726 17 23 1827 18 24 1928 19 25 2029 16 21 2230 17 22 2331 18 23 2432 19 24 2533 21 27 2234 22 28 2335 23 29 2436 24 30 2537 21 26 2738 22 27 2839 23 28 2940 24 29 3041 26 32 2742 27 33 2843 28 34 2944 29 35 3045 26 31 3246 27 32 3347 28 33 3448 29 34 3549 31 37 3250 32 38 3351 33 39 3452 34 40 3553 31 36 3754 32 37 3855 33 38 3956 34 39 4057 36 42 3758 37 43 3859 38 44 3960 39 45 4061 36 41 4262 37 42 4363 38 43 4464 39 44 4565 41 47 4266 42 48 4368 44 50 4569 41 46 4770 42 47 4871 43 48 4972 44 49 5073 46 52 4774 47 53 4875 48 54 4976 49 55 5077 46 51 5278 47 52 5379 48 53 5480 49 54 5581 51 57 5282 52 58 5383 53 59 5484 54 60 5585 51 56 5786 52 57 5887 53 58 5988 54 59 6089 56 62 5790 57 63 5891 58 64 5992 59 65 6093 56 61 6294 57 62 6395 58 63 6496 59 64 6597 61 67 6298 62 68 6399 63 69 64 100 64 70 65 101 61 66 67 102 62 67 68 103 63 68 69 104 64 69 70 105 66 72 67 106 67 73 68 107 68 74 69 108 69 75 70 109 66 71 72 110 67 72 73111 68 73 74112 69 74 75113 71 77 72114 72 78 73115 73 79 74116 74 80 75117 71 76 77118 72 77 78119 73 78 79120 74 79 80121 76 82 77122 77 83 78123 78 84 79124 79 85 80125 76 81 82126 77 82 83127 78 83 84128 79 84 852.矩阵求逆子程序调试2.1子程序源程序SUBROUTINE gjcp(A,N)integer Nreal A(N,N) !存放矩阵AINTEGER IP(N) !记录主列号REAL P !工作单元,放主元INTEGER I0,R !工作单元,放主列号EPS=0.001write(*,*)'gjcp ok0000000' DO K=1,NP=0I0=KIP(K)=KDO I=K,NIF(ABS(A(I,K)).GT.ABS(P))THENP=A(I,K)I0=IIP(K)=IENDIFENDDOIF(ABS(P).LE.EPS)THENWRITE(*,*)'DET=0'stop !后来加的GOTO 10ENDIFIF(I0.NE.K)THENDO J=1,NS=A(K,J)A(K,J)=A(I0,J)A(I0,J)=SENDDOENDIFA(K,K)=1./PDO I=1,NIF(I.NE.K)THENA(I,K)=-A(I,K)*A(K,K)DO J=1,NIF(J.NE.K)THENA(I,J)=A(I,J)+A(I,K)*A(K,J)ENDIFENDDOENDIFENDDODO J=1,NIF(J.NE.K)THENA(K,J)=A(K,K)*A(K,J)ENDIFENDDOENDDOwrite(*,*)'gjcp ok01'DO K=N-1,1,-1R=IP(K)IF(R.NE.K)THENDO I=1,NS=A(I,R)A(I,R)=A(I,K)A(I,K)=SENDDOENDIFENDDO10 write(*,*)'the end'END2.2考题1 1 2求矩阵-1 2 0 的逆矩阵。
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。