[Fortran] 纯文本查看 复制代码
program example
implicit none
integer, dimension(2, 3) :: a=reshape((/4, 9, 6, 7, 5, 3/), (/2, 3/))
integer, dimension(:, :), allocatable :: aLoc
integer :: temp
integer :: minLoc
integer :: errorLevel
integer :: i, j, k
allocate(aLoc, source=a, stat=errorLevel)
if ( errorLevel /= 0 ) stop ""
do i = 1, size(a, 1)
do j = 1, size(a, 2)
aLoc(i, j) = j
end do
end do
write(*, '(A,/(I1, ",", I1, ",", I1))') '输入矩阵:', ((a(i, j), j = 1, size(a, 2)), i = 1, size(a, 1))
write(*, '(A,/(I1, ",", I1, ",", I1))') '序号矩阵:', ((aLoc(i, j), j = 1, size(a, 2)), i = 1, size(a, 1))
do i = 1, size(a, 1)
do j = 1, size(a, 2)
minLoc = j
do k = j+1, size(a, 2)
if ( a(i, minLoc) > a(i, k) ) then
minLoc = k
end if
end do
if ( k /= j ) then
temp = a(i, minLoc)
a(i, minLoc) = a(i, j)
a(i, j) = temp
temp = aLoc(i, minLoc)
aLoc(i, minLoc) = aLoc(i, j)
aLoc(i, j) = temp
end if
end do
end do
write(*, '(A,/(I1, ",", I1, ",", I1))') '输入矩阵按行排序后:', ((a(i, j), j = 1, size(a, 2)), i = 1, size(a, 1))
write(*, '(A,/(I1, ",", I1, ",", I1))') '序号矩阵排序对应值:', ((aLoc(i, j), j = 1, size(a, 2)), i = 1, size(a, 1))
deallocate(aLoc, stat=errorLevel)
if ( errorLevel /= 0 ) stop ""
end program example
输出结果: