c    9.$BA]=PK!(B($B%,%&%9(B-$B%8%g%k%@%s$N>C5nK!(B)
c      $B?9@n(B $BLwBg(B , 22980138
c
      program ex9
c
      implicit none
      integer :: i,j,n
      real :: a(2,3),b(3,4),c(3,4)
c
      data (a(1,j),j=1,3)/ 4.0e0, 3.0e0, 0.0e0/
      data (a(2,j),j=1,3)/ 3.0e0, 2.0e0, 1.0e0/

      data (b(1,j),j=1,4)/ 2.0e0, 5.0e0,-3.0e0, 3.0e0/
      data (b(2,j),j=1,4)/ 4.0e0, 5.0e0,-3.0e0, 5.0e0/
      data (b(3,j),j=1,4)/-1.0e0,-5.0e0, 4.0e0, 1.0e0/

      data (c(1,j),j=1,4)/ 1.0e0, 1.0e0, 1.0e0, 3.0e0/
      data (c(2,j),j=1,4)/ 2.0e0, 3.0e0, 4.0e0, 5.0e0/
      data (c(3,j),j=1,4)/ 1.0e0, 2.0e0, 3.0e0, 2.0e0/

      write(6,*)'9.$BA]=PK!(B($B%,%&%9(B-$B%8%g%k%@%s$N>C5nK!(B)'
      write(6,*)''
      write(6,*) '(1)'
      write(6,*)'$B=i4|CM(B'
      do i=1,2
         write(6,100) i,'   ',(a(i,j),j=1,3)
      enddo
      n=2
      call hakidashi(a,n)

      write(6,*)''
      write(6,*) '(2)'
      write(6,*)'$B=i4|CM(B'
      do i=1,3
         write(6,100) i,'   ',(b(i,j),j=1,4)
      enddo
      n=3
      call hakidashi(b,n)

      write(6,*)''
      write(6,*) '(3)'
      write(6,*)'$B=i4|CM(B'
      do i=1,3
         write(6,100) i,'   ',(c(i,j),j=1,4)
      enddo
      n=3
      call hakidashi(c,n)

      stop
  100 format(1x,i2,1x,a3,1x,f7.4,1x,f7.4,1x,f7.4,1x,f7.4) 
      end

c-----$BA]=PK!$N%5%V%k!<%A%s(B
      subroutine hakidashi(a,n)
      implicit none
      integer :: i,j,k,n,m,l,maxi
      real :: aii, aik, maxa
      real :: a(n,n+1),d(n+1),ma(4)
      
      do k=1,n
          maxi=k
          do i=k,n
             if ( abs(a(i,k)) > abs(a(maxi,k)) ) then
                maxi=i
             endif
          enddo
          do j=k,n+1
             d(j)     =a(maxi,j)
             a(maxi,j)=a(k,j)
             a(k,j)   =d(j)
          enddo
          if (maxi/=k) then
             write(6,*)''
             do m=1,n
              write(6,100) m,'   ',(a(m,l),l=1,n+1)
              enddo
          end if
c         do i=k,n
c           ma(i)=abs(a(i,k))
c         enddo
c         maxa = max(ma(1),ma(2),ma(3),ma(4))
c         do i=k,n
c          if (ma(i) == maxa) then
c             do j=k,n+1
c                d(j)  =a(i,j)
c                a(i,j)=a(k,j)
c                a(k,j)=d(j)
c             enddo
c             if (i/=k) then
c                write(6,*)''
c                do m=1,n
c                 write(6,100) m,'   ',(a(m,l),l=1,n+1)
c                enddo
c             end if
c          end if
c         enddo
         aii = a(k,k)
         if (aii== 0.0e0 .and. a(k,n+1)==0.0e0) go to 10
         if (aii== 0.0e0 .and. a(k,n+1)/=0.0e0) go to 20
         do j=k,n+1
            a(k,j) = a(k,j)/aii
         enddo
         write(6,*)''
         do m=1,n
            write(6,100) m,'   ',(a(m,l),l=1,n+1)
         enddo
         do i=1,n
            if( i /= k ) then
                aik = a(i,k)
                do j=k,n+1
                   a(i,j) = a(i,j) - aik*a(k,j)
                enddo
                write(6,*)''
                do m=1,n
                   write(6,100) m,'   ',(a(m,l),l=1,n+1)
                enddo
            endif
         enddo
      enddo
      write(6,*)''
      write(6,*)'$B2r(B'
      write(6,'(a3,f7.4)')'x=',a(1,n+1)
      write(6,'(a3,f7.4)')'y=',a(2,n+1)
      if (n==3) then
      write(6,'(a3,f7.4)')'z=',a(3,n+1)
      endif
      go to 30
   10 write(6,*)''
      write(6,*)'$B2r$OITDj(B'  
      go to 30
   20 write(6,*)'$B2r$OITG=(B'
   30 continue  
      return
  100 format(1x,i2,1x,a3,1x,f7.4,1x,f7.4,1x,f7.4,1x,f7.4) 
      end
