前言
Fortran语言是我接触过最古老的编程语言之一了,它很古老,所以很Geek。
一、Fortran题目
1、求两数的最大公约数和最小公倍数。
2、对任意自然数 n,设计一个求 n 的各位数字的立方和的函数子程序 F(n)
(如 F(121)=13+23+13=1+8+1=10),并调用 F(n)求 1000 以内的所有水仙花数
(当一个数各个数位的立方和等于这个数本身时,称这样的数为水仙花数)。
3、判断输入字符串是否为“回文”,即顺读与倒读相同,如 level 为回文。
4、二维数组运算,包括加、减、乘、乘逆。
5、蒙特卡洛求解 π 值。
6、高斯消元解方程组。
二、代码
1.第一题
1、求两数的最大公约数和最小公倍数。
Program Finite_Element
integer :: m,n,s,temp
m = 100
n = 200
s=m*n
if(m<n)then
temp=m;m=n;n=temp
end if
do while(mod(m,n)/=0)
temp=n;n=mod(m,n);m=temp
end do
write(*,"('最大公约数为:'i4)") n
write(*,"('最小公倍数为:'i4)") s/n
end
2.第二题
2、对任意自然数 n,设计一个求 n 的各位数字的立方和的函数子程序 F(n)
(如 F(121)=13+23+13=1+8+1=10),并调用 F(n)求 1000 以内的所有水仙花数
(当一个数各个数位的立方和等于这个数本身时,称这样的数为水仙花数)。
program main
implicit none
integer(kind=4):: x
integer F
read(*, *) x
print *,'输入的数是',x
print *,' 各数字立方和为',F(x)
IF (x==F(x)) THEN
print *, '该数是水仙花数'
else
print *, '该数不是水仙花数'
end if
end program
integer function F(x)
implicit none
integer :: x
integer, parameter :: N = 3
integer(kind=4) :: num = 0
!求余不等于0,除以10不小于0
do while( mod(x,10) /= 0 .and. x/10.0 > 0.00)
!print*, x/10
num=num+mod(x,10)**N
x=x/10
end do
!print*, x/10
F=num
end function
3.第三题
3、判断输入字符串是否为“回文”,即顺读与倒读相同,如 level 为回文。
program main
implicit none
character(len=100)::chr
integer(8)::N,k,i
write(*,*)'请输入一个整数:';read(*,*)N
if(IsPalindrome(N)) then
write(*,*)N,"是回文数"
else
write(*,*)N,"不是回文数"
endif
write(*,*)'输出完成!'
read(*,*)
contains
function IsPalindrome(N)result(p)
implicit none
integer(8),intent(in)::N
integer(8)::i,k
logical(1)::p
character(len=100)::chr
p=.true.
write(chr,"(I100)")N
chr=adjustl(chr)
k=len_trim(chr)
do i=1,k/2,1
if(chr(i:i)/=chr(k-i+1:k-i+1))then
p=.false.
exit
endif
enddo
end function IsPalindrome
end
4.第四题
4、二维数组运算,包括加、减、乘、乘逆。
program main
implicit none
integer(kind=4) :: x,y
character :: op
real :: num
print *, "请输入运算式"
read(*,*) x,op,y
select case (op)
case('+')
print *,x+y
case('-')
print *,x-y
case('*')
print *,x*y
case('/')
print *,x/y
case('^')
if(y>0)then
print *,x**y
else
num = 1.0/x**(-y)
print *,num
end if
end select
end program
5.第五题
5、蒙特卡洛求解 π 值。
传说中,越底层的语言运行的越快,我感觉fortran在这个任务中,运算速度确实极快,很快的就能算出pi
program estimate_pi
implicit none
float length, r, rand_x, rand_y, radius, area_square, square_points,
circle_points, origin_dist, approx_pi
integer i, iterations
length = 1
radius = length/2
area_square = length * length
square_points = 0
circle_points = 0
iterations = 10000000
i = 0
do while (i < iterations)
call random_number(r)
rand_x = - length/2 + length * r
call random_number(r)
rand_y = - length/2 + length * r
origin_dist = rand_x * rand_x + rand_y * rand_y
if (origin_dist <= radius * radius) then
circle_points = circle_points + 1
end if
square_points = square_points + 1
approx_pi = 4 * (circle_points/square_points)
i = i + 1
end do
print*, 'Approximate value of pi is', approx_pi
print*, 'Number of points in circle', circle_points
print*, 'Number of points in square', square_points
end program estimate_pi
6.第六题
6、高斯消元解方程组。
代码过长此处省略(其实是我不会)
在这里插入代码片
总结
贴主最精通的语言是python,从python直接过度到fortran你就发现,古老的语言真难学,社会的进步多伟大。