用VFP编写程序找出 1000 以内的所有完数,并按下面格式输出:6 its factors are 1,2,3

2025-05-16 14:14:22
推荐回答(2个)
回答1:

调试通过的程序代码如下:

dimension f(1000)

for i=1 to 1000

  k=1

  s=0

  for j=1 to i-1

    if mod(i,j)=0 then

      f(k)=j

      s=s+j

      k=k+1

    endif

  endfor

  if i=s then

    ? i, 'its factors are 1'

    for j=2 to k-1

      ?? ',',f(j)

    endfor

  endif

endfor

上面的程序运行结果见贴图

回答2:

LOCAL i,j,k,strTemp

for i=1 to 1000
k=0
strTemp=""
for j=1 to i-1
if i % j=0
k=k+j
strTemp = Strtemp + alltrim(str(j))+","
endif
endfor
if k=i
?str(i) +" its factors are :" +left(strTemp,len(strtemp)-1)
endif
endfor