你的A應該是指整理過後的Y吧
我從頭(X,Y)開始寫一個解答
基本上排序有點偷懶,就簡單判斷X(i)>X(j)就代表所有排序在j之後的元素
其他有問題再提出來吧。
function get_P(X,Y) result(P)
implicit none
! input
real, intent(in), allocatable :: X(:), Y(:)
integer :: i, j
! output
integer :: P
P=0
do i=1,size(X)
do j=1,size(X)
if(X(i) > X(j) .and. i /= j) then
if(Y(i)>Y(j))then
P=P+1
end if
end if
end do
end do
end function get_P
function get_M(X,Y) result(M)
! input
real, intent(in), allocatable :: X(:), Y(:)
integer :: i
! output
integer :: M
M=0
do i=1,size(X)
do j=1,size(X)
if(X(i) > X(j) .and. i /= j) then
if(Y(i)<Y(j))then
M=M+1
end if
end if
end do
end do
end function get_M
program project01
implicit none
real,allocatable :: X(:),Y(:)
integer :: maxsize, i
interface
function get_P(X,Y)
real, dimension(:) :: X,Y
integer :: get_P
end function get_P
function get_M(X,Y)
real, dimension(:) :: X,Y
integer :: get_M
end function get_M
end interface
integer :: P,M,S
maxsize=110
allocate( X(maxsize),Y(maxsize) )
X=0.0d0
Y=0.0d0
!open(10,file='mtempnorth.txt')
do i=1,maxsize
!read(10,*) A(i)
! get example
X(i)=rand()
Y(i)=rand()
end do
!close(10)
P=get_P(X,Y)
M=get_M(X,Y)
print*, P-M
stop
end