|
考虑到 Fortran 是一门向量化的语言,给出一个高级的版本,非常简单且高效。 [Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode
program hello
implicit none
integer n,s
subroutine fcount(n,s)
s=0
do i=1,n
s=s+1./i
end do
end
call fcount(50,s1)
call fcount(100,s2)
call fcount(150,s3)
call fcount(200,s4)
s=s1+s2+s3+s4
print *, "s=", s
end program
|
vvt 发表于 2021-4-18 16:19 非常感谢 |
|
程序单元里不能直接放入另一个程序单元,需要符合一定的规则。 [Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode program hello implicit none write(*,*) "s=", sum(fcount([50,100,150,200])) contains Elemental Real Function fcount(n) result( s ) integer , intent(IN) :: n integer :: i s=sum(1./[(i,i=1,n)]) End Function fcount End program hello |
捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )
GMT+8, 2025-11-3 18:48