求助:FORTRAN计算组合问题出错
本帖最后由 guxwu 于 2010-1-3 17:15 编辑老大们,这是一个计算N个数里面提取M个数的所有组合问题,本人菜鸟,没有看出哪里出现问题,使得程序运行结果全是0,跪求高手指点一二.
program example
implicit none
integer n,m,cnm,begin,depth,num,i,j
common num
integer,allocatable::output(:),counting(:)
real,allocatable::a(:)
open(unit=1,file="input.txt")
open(unit=2,file="output.txt")
begin=1
depth=1
num=1
read(1,*) n,m,cnm
allocate(counting(m))
allocate(output(cnm))
allocate(a(n))
read(1,*)(a(i),i=1,n)
call iter(n,m,cnm,begin,depth,counting,output,a)
write(2,*)output
end
recursive subroutine iter(n,m,cnm,begin,depth,counting,output,a)
implicit none
integer n,m,cnm,begin,depth,num
common num
integer::output(cnm)
integer::counting(m)
real::a(n)
integer i,j
if(depth/=m) then
do i=begin,n-m+depth
counting(depth)=i
call iter(n,m,cnm,counting(depth)+1,depth+1,counting,output,a)
end do
else
do i=begin,n-m+depth
counting(depth)=i
end do
do j=1,m
output(num)=a(counting(j))
num=num+1
end do
end if
return
end program example
implicit none
integer n,m,cnm,begin,depth,num,i,j
common num
integer,allocatable::output(:),counting(:)
real,allocatable::a(:)
open(unit=1,file="input.txt")
open(unit=2,file="output.txt")
begin=1
depth=1
num=1
read(1,*) n,m,cnm
allocate(counting(m))
allocate(output(cnm))
allocate(a(n))
read(1,*)(a(i),i=1,n)
call iter(n,m,cnm,begin,depth,counting,output,a)
write(2,*)output
end
recursive subroutine iter(n,m,cnm,begin,depth,counting,output,a)
implicit none
integer n,m,cnm,begin,depth,num
common num
integer::output(cnm)
integer::counting(m)
real::a(n)
integer i,j
if(depth/=m) then
do i=begin,n-m+depth
counting(depth)=i
call iter(n,m,cnm,counting(depth)+1,depth+1,counting,output,a)
end do
else
do i=begin,n-m+depth
counting(depth)=i
end do
do j=1,m
output(num)=a(counting(j))
num=num+1
end do
end if
return
end
页:
[1]