過去ログ

「数学」小旅行
#51352 私もRubyプログラムでやってみました。再帰呼び出しを使って。

算チャレ1203類題(1)はg1で、(2)はg2がその関数です。

$u=21;$r=23;$c1=0;$c2=0

def g1(a,b,k,l)
if a==$u&&b==$r then $c1=$c1+1
else if a<$u then;if l!=1 then g1(a+1,b,l,1)end end
if b<$r then g1(a,b+1,l,2)end end
end

def g2(a,b,k,l)
if a==$u&&b==$r then $c2=$c2+1
else if a<$u then;if l!=1 then g2(a+1,b,l,1)end end
if b<$r then;if k+l!=4 then g2(a,b+1,l,2)end end end
end

p Time.now;g1(0,0,0,0);p Time.now;p $c1
p Time.now;g2(0,0,0,0);p Time.now;p $c2

で、結果は
08:42:52.2405349
08:42:56.6741753
2024

08:42:56.6754296
08:42:56.7551062
1582

でした。
   3月11日(金) 9:01:25     51354

過去問題一覧へ戻る