AP過去問 令和7年度春期 午前 問7
AP過去問 令和7年度春期 午前 問6前の問題へ
AP過去問 令和7年度春期 午前 問8次の問題へ
問7(問題文)
fact(n)は、非負の整数nに対してnの階乗を返す。fact(n)の再帰的な定義はどれか。
ア if n=0 then return 0 else return n*fact(n-1)
イ if n=0 then return 0 else return n*fact(n+1)
ウ if n=0 then return 1 else return n*fact(n-1)
エ if n=0 then return 1 else return n*fact(n+1)
回答・解説
再帰的に階乗を求める関数fact(n)において、階乗の定義は以下のようになります。
0の階乗は1です。つまり、fact(0)=1です。
n>0のとき、fact(n)=n×fact(n−1)となります。
この定義に従うと、正しい再帰定義は以下の通りです。
ウ if n=0 then return 1 else return n*fact(n-1)
したがって
ウ
が答えです。
AP過去問 令和7年度春期 午前 問6前の問題へ
AP過去問 令和7年度春期 午前 問8次の問題へ