-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathex0829.f90
35 lines (32 loc) · 959 Bytes
/
ex0829.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
program ex0829
implicit none
integer :: n
integer, external :: fact
write(*,*) 'N='
read(*,*) n
write(*, "(I2,'! = ',I8)" ) n, fact(n)
stop
end
recursive integer function fact(n) result(ans)
implicit none
integer , intent(in) :: n
integer, save :: count = 1
integer :: localcount, temp ! 局部变量
localcount = count
count = count+1
write(6,"(I2,'th enter, n=',I2)") localcount, n
if ( n < 0 ) then ! 不合理的输入
ans = -1 ! 随便设定一个值
write(6,"(I2,'th exit, n=',I2,' ans=',I8)") localcount, n, ans
return ! n不合理, 直接return
else if ( n <= 1 ) then
ans = 1
write(6,"(I2,'th exit, n=',I2,' ans=',I8)") localcount, n, ans
return ! 不用再向下递归了, return
end if
! 会执行到这, 代表n>1, 从n*(n-1)!来计算n!
temp = n-1
ans = n * fact(temp)
write(6,"(I2,'th exit, n=',I2,' ans=',I8)") localcount, n, ans
return
end