program main implicit none integer :: n complex*16, allocatable :: a(:,:) integer, allocatable :: b(:) real*8 :: s1,s2,s3,re,im complex*16 :: tmp integer :: i,j !1) open(10,file='input') read(10,*) n if (n.lt.2) then write(*,*) 'm must be at least 2' stop endif allocate(a(n,n)) do i=1,n do j=1,n read(10,*) re,im a(i,j)=dcmplx(re,im) enddo enddo close(10) !2) s1=0.d0 do i=n,1,-1 s1=s1+dble(a(i,n+1-i)) enddo s2=0.d0 do i=1,n do j=i+1,n s2=s2+aimag(a(i,j)) enddo enddo s3=0.d0 do i=1,n s3=s3+aimag(a(i,1)) enddo do i=1,n s3=s3+aimag(a(i,n)) enddo write(*,*) 's1-s2', s1-s2 write(*,*) 's2-s3', s2-s3 !3) if ((s1-s2).gt.(s2-s3)) then do i=1,n-1 do j=i+1,n tmp=a(i,j) a(i,j)=a(j,i) a(j,i)=tmp enddo enddo a=conjg(a) else allocate(b(n)) do i=1,n b(i)=int(dble(a(i,i))) enddo call sort(b,n) endif !4) if ((s1-s2).le.(s2-s3)) then open(11,file='b.dat') do i=1,n write(11,100) i,b(i) enddo close(11) deallocate(b) else open(11,file='output.dat') do i=1,n do j=1,n write(11,101) i,j, dble(a(i,j)),aimag(a(i,j)) enddo enddo endif 100 format(2(I5)) 101 format(2(I5),2(F10.3)) deallocate(a) stop end program main subroutine sort(a,n) implicit none integer, intent(in) :: n integer, intent(inout) :: a(n) integer :: i,j do i=1,n do j=2,n-(i-1) if (a(j-1).gt.a(j)) then call scambia(a(j-1),a(j)) endif enddo enddo return end subroutine sort subroutine scambia(x,y) implicit none integer, intent(inout) :: x,y integer :: tmp tmp=x x=y y=tmp return end subroutine scambia