program main implicit none integer :: n, i, i2, i3, j, ii integer, allocatable :: a(:), b(:), c(:) integer :: sum_b, sum_c, tmp, tmp1 integer, allocatable :: d(:), m(:,:) ! 1) do write(*,*) 'Insert n' read(*,*) n if (n.gt.10) exit enddo allocate(a(n)) open(10,file='a.dat') do i=1,n read(10,*) a(i) enddo close(10) ! 2) i3=0 i2=0 do i=1,n if (mod(i,3).eq.0) i3=i3+1 if (mod(i,2).eq.0) i2=i2+1 enddo write(*,*) 'i3', i3 write(*,*) 'i2', i2 allocate(b(i3)) allocate(c(i2)) ii=0 j=0 do i=1,n if (mod(i,3).eq.0) then ii=ii+1 b(ii)=a(i) endif if (mod(i,2).eq.0) then j=j+1 c(j)=a(i) endif enddo sum_b=sum(b) sum_c=sum(c) write(*,*) 'sum_b', sum_b write(*,*) 'sum_c', sum_c ! 3) if (sum_b.gt.sum_c) then allocate(d(i3+i2)) d(1)=b(1) do i=1,i2 d(i+1)=c(i) enddo do i=1,i3-1 d(i2+1+i)=b(i+1) enddo else allocate(m(n,n)) do i=1,n m(i,i)=a(i) enddo tmp=sum_b*sum_c tmp1=sum_c-sum_b do i=1,n do j=i+1,n m(i,j)=tmp enddo enddo do i=1,n do j=i+1,n m(j,i)=tmp1 enddo enddo endif ! 4) open(10,file='b.dat') open(11,file='c.dat') do i=1,i3 write(10,100) i, b(i) enddo do i=1,i2 write(11,100) i, c(i) enddo close(10) close(11) if (sum_b.gt.sum_c) then open(10,file='d.dat') do i=1,i3+i2 write(10,100) i,d(i) enddo else open(10,file='m.dat') do i=1,n do j=1,n write(10,101), i,j,m(i,j) enddo enddo endif close(10) 100 format(I4,I4) 101 format(I4,I4,I6) deallocate(a) deallocate(b) deallocate(c) if (sum_b.gt.sum_c) then deallocate(d) else deallocate(m) endif stop end program main