[Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode
ncnt = 0 !计数器,非零元素的个数
do i = 1,n
do j = 1,n
if (A(i,j) /=0) then
ncnt =ncnt +1
endif
enddo
enddo
allocate(vec(ncnt))
ncnt = 0
do i = 1,n
do j = 1,n
if (A(i,j) /=0) then
ncnt =ncnt +1
vec(ncnt) = A(i,j)
endif
enddo
enddo
[Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode
program www_fcode_cn
implicit none
integer , allocatable :: a(:) , b(:)
allocate( a(9) )
a = [1,2,0,0,4,0,0,0,8,0]
! 如果你的编译器不支持,请加入以下语句
!allocate( b( count(a/=0) ) )
b = pack(a,a/=0)
write(*,*) b
end program www_fcode_cn